AlfieWebDevTips Homefor new and experienced developers
[Home Home] [Web Development Web Development ] [Code Generators Code Generators ] [Resources Resources ] [Contact Us Contact ] |WebDevTips Privacy Privacy |
Print This Page

You are here : Home - Web Development - Javascript - Getting Started - Hello World!

Hello World!



Example 1

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 2

For 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"> 
<!-- 
document.write("Hello World!"); 
// -->
 
</script> 
<noscript>Hello World written to the page using javascript</noscript>

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




16 Nov 2003




Google
 

[back to top of page]   

[labelled with icra -  - this link will open in a new window] [RSS Content]

[Copyright © WebDevTips]