In this tutorial we will see how to make our first Javascript program using the Notepad ++ IDE. This program will display the famous "Hello World!" Message.
1 - Structure of a Javascript code
The syntax of a JavaScript code is of the form:
.
<script language="javascript">
Your code Javascript here !
</script>
or :
<script type="text/javascript">
Your code Javascript here !
</script>
Example (displaying a message on the screen)
You can display a message on the screen using the alert () function; )
<script type="text/javascript">
alert ('Hello World!');
</script>
We can now wonder where are we going to insert this code?
There are two methods available to you:
- 1st method (directly between the body and / body tags)
- 2nd method (Javascript code between Head and Head tags)
In this tutorial we will see the first only, the 2nd will be treated in the next chapter
Example (Javascript code between Body and / Body:
<! DOCTYPE html>
<Html>
<Head>
<title> Hello World! </ title>
</ Head>
<Body>
<script language = "javascript">
alert ('Hello world!');
</ Script>
</ Body>
</ Html>
This Javascript code is inserted directly between the body and / body tags, which means that it will be
executed directly when the page is loaded. To test this example :
- Launch your notepad ++ editor
- Copy the code above and paste it on Notepad ++
- From the Language menu choose HTML
- Save the file by choosing a name of your choice, for example myFirstProgram
To run the code now, click on the Run menu and choose Launch in (your Browser), for example Launch in firefox
At this moment a small window displaying the message Hello World! appears on the screen.