JavaScript Tutorial For Beginners
JavaScript is a scripting language designed to add interactivity to your HTML pages.
You can easily insert JavaScript into your web pages by inserting these tags into your web page :
<script type="text/javascript">
Script Code Inserted Here
</script>
The part in red shows the beginning of the script and the part in blue shows to end of the script.
If you want to know how to write text using JavaScript, then you can view the example below :
<html>
<body>
<script type="text/javascript">
document.write("I was not here");
</script>
</body>
</html>
Now, you might wonder, if you can use HTML tags in your script. Below is an example on how to add HTML tags to script using JavaScript.
<html>
<body>
<script type="text/javascript">
document.write("<b>I was not here</b>");
</script>
</body>
</html>
As you can see from the above examples, the 'document.write' JavaScript command is used for writing text on your web pages.
It is a JavaScript command because it's between the opening and closing JavaScript tags. ( as shown above in the example )
Scripts can also be executed from the head tags of your web page. Example of simple pop up alert box from using JavaScript.
<html>
<head>
<script type="text/javascript">
function popup()
{
alert("This is a pop up box onload from the body tag");
}
</script>
</head>
<body onload="popup()">
</body>
</html>
You can remove <onload="popup()"> from the body tags of your web page and use this below between the opening and closing tags of your body tags to have the same effect as the above script :
<input type="button" value="Click Here" onclick="popup()" />
Or...
<a href="#" onclick="popup()">Click Here</a>
There are times that you feel that your head tags are filled with scripts or you want to use the same script on multiple web pages but don't want to keep inserting the script on each web page, so you can use an external JavaScript file to store these scripts and use them on your web pages.
All that you are required to do, is to create a new text file on your computer and change the file extension to .js
To use the external JavaScript file, point the JavaScript file to the 'src' attribute of this code below :
<script type="text/javascript" src="filename.js"></script>
In the code above, I've named my JavaScript file, 'filename.js' . Replace that name with your JavaScript external file and insert this code into the HEAD tags of your web page.
