JavaScript code inside HTML tags.How to add JavaScript code to our HTML pages? Check this link about structure of a simple html code. As html is a set of tags to format our web page, similarly we can use <script> tag to execute any script. To run our JavaScript code the client browser script execution setting must be enabled. As we are keeping our JavaScript code inside html pages by using <script> tag, it is upto us to keep our code in any location. However if we want our code to work as soon as the page loads, then we have to keep our JavaScript code inside <head> tag. Here is an sample of simple JavaScript code kept inside head tag.
The above code will write Hello World as soon as the page loads to the client browser. We can keep the script inside our body tag also . Like this
Let us change this a bit and use one alert message inside a function. Like this.
You can see above code will not display the alert message because we have kept the message inside a function and we need to call the function. To call the function onload we have to use the body tag like this.
Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
| ||||||||
More on Loops and Variables
| ||||||||