11.14.07
The JavaScript Onload Function
JavaScript is a very interesting programming language to use simply because it is a language that is so easy to use in conjunction with HTML and other web languages. JavaScript was initially designed for the web and is made even better for the web whenever it is updated, so it is quite easy to see why people are impressed with the JavaScript language. There are a number of things that you can do in order to make your website better with the JavaScript functions and therefore it is important for people to realize exactly what those functions happen to be.
For starters, one of those functions is known as the onload function. This is one of the earliest taught functions for JavaScript because of not only the ease of learning that it brings to the table, but also because it is one of the more basic functions around. Onloads will be big parts of larger chunks of JavaScript code and this is why it is important that people learn about them early on within their JavaScript training.
Now, like most pieces of code in JavaScript, it serves no purpose to verbally explain what the onload function does without using an example in order to help that illustration along. To that end, take a look at the following piece of code:
<html>
<head>
<script type=”text/javascript”>
Function message()
{
Alert(“Hello World”)
}
</script>
</head>
<body onload=”message()”>
</body>
</html>
Have you ever seen a prompt window pop up when you get to a website with a message for you to read and then at least one option button (i.e. the okay button, although sometimes there is more than one option depending on what the message says) for you to click? Well, if you stuck this little bit of code into your website, anyone that visited it would have a pop-up message that said “Hello World.”
This is done through the use of the message() function, but that is not the part of the code we are predominantly interested in. We are interested in the onload attribute of the body tag, which happens to execute the function that was defined within the head part of the HTML code. Attaching the onload attribute to the body tag ensures that the message() function is executed as soon as the site is displayed. Message() is not the only function that can be onloaded and therefore you can see easily now why the onload attribute is an important one to learn.
Scriptycan is a great software repository featuring both free and commercial JavaScript scripts and applications for developers and programmers.