Our first script will be the customary Hello World! script.
First we will make the text appear in an alert box. For more information on dialogue boxes you can read our complete guide but for now you can check out example 1.
<a href="#" onclick="alert('Hello World!');">Click on me</a>You will notice that, in this example there are no script tags to be seen. The javascript alert is triggered by an onclick event. We will be discussing events later in the guide. In the meantime the part we are interested in is the alert('Hello World!');
alert() is a built in javascript function that displays a dialogue box with the text specified between the ' ', in our example it is Hello World! Try changing the text to get a feel for how the alert works.
Example 2For our next example we will write our text to the webpage using javascript. Take a look at example 2
<script language="JavaScript" type="text/javascript">This example introduces document.write and it instructs the browser to write whatever is between the quotes to the page. This script block must be placed where you want the text to appear in the document.
The script as it stands is pretty useless as we could have done that with normal html but as you progress you will see how useful document.write can be. For a practical use of document.write take a look at adding the date to your page.
Notice the use of the <noscript></noscript> tags. As mentioned before, get into the habit of using them now.
Next we'll take a look at events