Now view this file in any modern browser to execute the script. This script will show a Message box showing the text "Hello World".
One can also store the script in another file. In such a case the script tag will be the following
where 'file.js' is the file in which the script is stored in.
|<a onClick="alert('JS Statement')">Click Me</a>||Click Me|
NOTE : This are the traditional or old event handler. There are more event handlers specified in W3C's recommendations - more of that later.
All the above methods will wait for the user to do something - like clicking the link or the button or whatever. If you want to execute a statement when the page is loaded, use the following command.
<body onLoad="alert('JS Statement')">
There are many more methods and 'on' statements, as I like to call them, to do this task. The major 'on' statements are given below. These should be given in the place of 'onClick' in the above examples.To be used with links, images, etc.
Anywhere inside the body
To Be Used with Body Tag
|<a href="#" onMouseOver="alert('Hello World')">Move the mouse over this link</a>||Move the mouse over this link|