Sunday, November 2, 2008

Using Multiple JavaScript Onload Functions

Using Multiple JavaScript Onload Functions

JavaScript is one of the most popular programming languages in use on the World Wide Web. It can create interactive pages through the use of menus, forms and calendars. It can also be used to track a visitor's history on your site, play games and many other things.

When scripts are written they're used to accomplish a given task, e.g., create a rotating picture gallery, or to validate a form. This means that for each different type of usage, a separate script is necessary. Often, a script is called using an onload function.
Loading the Page

When a Web page is loaded, the code on the page — whether HTML, CSS, PHP, or some other type — is generally processed from the top down. As the browser comes upon a section of code, it's executed and the intended action is (hopefully) performed. Let's say you have the following script in the head section of a Web page:

1 function func1() {
2 alert("This is the first.");
3 }
4
5 window.onload=func1;
view plain | print | ?
function func1() { alert("This is the first."); } window.onload=func1;

When the page is loaded, the browser will stop at the script above and execute it. An alert window will pop up and the page will stop loading until the alert window is closed. Often, this isn't an issue, but if the script needs to access elements on the page that aren't yet loaded (i.e., below the current code), you will encounter problems. To resolve these, here are a few solutions.
Move it to the Bottom

One method is to place the script at the bottom of the Web page, just above the closing </body> tag. This does, however, present a few problems. First, there is a period of time that those elements which are you seeking to change with the script will be clearly seen by the visitor. Then, once the script is executed, they will "magically" change. With a fast loading page and a script that executes quickly, this might not be a problem, but if the original elements can be seen by the visitor, and are then changes, the visitor may perceive it to be some type of a hack or bug on the page, and leave the site immediately. Obviously, that's not good.

Secondly, either the script, or a link to it, will need to be included in the actual HTML body code. That's not unobtrusive JavaScript. It's best if you can keep everything separate. You'll find it's easier to work on any aspect of the page, and the page itself will load faster.
The onload Function

The window object in JavaScript has an event handler called onload. When this event handler is used, the entire page and all of its related files and components are loaded before the function listed in the onload event handler is executed, hence the term "on load."

As shown in the script above, it's easy to use, but what if you have more than one function you want to call using an onload event handler?

You would think you could just stack them like this:
1 window.onload=func1;
2 window.onload=func2;
3 window.onload=func3;
view plain | print | ?
window.onload=func1; window.onload=func2; window.onload=func3;

Unfortunately, it's not that simple. After the first onload event handler is executed, it will be replaced when the second onload event handler is executed. That, in turn, will be replaced immediately just as soon as the third onload event handler is executed. There are workarounds for this, though.
Let's Put Them in a Chain

One method that has been used quite a bit is the linking of multiple events. It places several function calls in a chain, using one onload event handler. The method would look like this:
<body onload="func1(); func2();">
<body onload="func1(); func2();">

Still, once again we run into the unobtrusive problem. In addition, you will need to add this to the <body> tag of every page that needs to use the called functions. That could be quite tedious for a large Web site, especially when changes or additions are needed.
Within Another Function

Another method is the following script:
1 function start() {
2 func1();
3 func2();
4 }
5
6 window.onload = start;
view plain | print | ?
function start() { func1(); func2(); } window.onload = start;

While this is good, there's one method that's even better.
The addLoadEvent Function
1 function addLoadEvent(func) {
2 var oldonload = window.onload;
3 if (typeof window.onload != 'function') {
4 window.onload = func;
5 } else {
6 window.onload = function() {
7 if (oldonload) {
8 oldonload();
9 }
10 func();
11 }
12 }
13 }
14
15 addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);
16 addLoadEvent(function() {
17 /* more code to run on page load */
18 });
view plain | print | ?
function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { if (oldonload) { oldonload(); } func(); } } } addLoadEvent(nameOfSomeFunctionToRunOnPageLoad); addLoadEvent(function() { /* more code to run on page load */ });

This function was written by Simon Willison. It has several advantages. For one, it's really unobtrusive. It can be placed in a file with your other scripts or in a separate file. (If it's in a separate file, just be sure to call it after the other files, so the functions will be available.)

Also, it works even if the onload event handler has been previously assigned. Simon explains it like this:

"The way this works is relatively simple: if window.onload has not already been assigned a function, the function passed to addLoadEvent is simply assigned to window.onload. If window.onload has already been set, a brand new function is created which first calls the original onload handler, then calls the new handler afterwards."

Isn't that beautiful? So, if you already have a script that uses the onload event handler, you don't need to dig it out and change it, unless you want to. It also allows for extra code. Here's an example that calls two functions and adds a third, independent addLoadEvent:
1 function func1() {
2 alert("This is the first.");
3 }
4
5 function func2() {
6 alert("This is the second.");
7 }
8
9
10 function addLoadEvent(func) {
11 var oldonload = window.onload;
12 if (typeof window.onload != 'function') {
13 window.onload = func;
14 } else {
15 window.onload = function() {
16 if (oldonload) {
17 oldonload();
18 }
19 func();
20 }
21 }
22 }
23
24 addLoadEvent(func1);
25 addLoadEvent(func2);
26
27 addLoadEvent(function() {
28 document.body.style.backgroundColor = '#EFDF95';
29 })



After calling func1 and func2, a function is created that changes the background color of the Web page. Each of these is executed in the order shown. Try it yourself and see how it works!
Conclusion

Sometimes, figuring out which approach to use in coding takes a bit of trial and error. Other times it's advantageous to search the Web and see what others have done. The addLoadEvent function should work just fine in this situation. We use it over at the JavaScript Source for all of our scripts requiring the onload event handler.

2 comments:

Vaibhavi said...

Hi all,

I am working for a software integrator company. My projects includes working on Java and Ruby on Rails and Ajax. I think Web Services is really cool. We also recently have to now work on REST and they are talking about mashups and Struts. Can anyone tell me if there are some good training or conferences so that me and my team members can get to speed with these technologies. Learning from books is not my cup of tea, even not when I was doing engineering ;)

All the help that group members can provide in this regard is much appreciated.

Thanks,
Vaibhavi

anaz said...

Hi Vaibhavi,

There are several online resources available that you just google for. If any of your team like to read then quality books from wiley and oreilly cover such technologies in detail.

I also highly recommend you could attend the upcoming Great Indian Developer Summit (http://www.developersummit.com) that is covering Java, Agile, REST, JAX-RS, mashups, .NET, Rich Web, JPA, SOA, rich user experiences, Spring, Groovy and more. They have most of the creators of these technologies as speakers. My team is attending this summit 22-25 apr at IISc campus where we are attending the web conference on April 23 and java on April 24. We have been able to get very good discounts. Maybe all those who are interested from your group can sign up together and get a good bargain from them. what say? I also attended last year's conference and had a really cool time.

In Hyderabad there is Sun Tech Days with some sun speakers.

Thanks,
Anaz