↑ Grab this Headline Animator

Thursday, May 14, 2009

How to call multiple Javascript methods on window load using onload handler

We know that when we cannot assign mutilple functions to onload handler in javascript. It will just ovveride the previous function.

In order to set mutliple functions we need to consider Array and add all methods to that array.(functions can be added in Page or Usercontrol).

First Create onloadManager array.

if (!window.onloadManager)
window.onloadManager = new Array();

//add existing method to array

if (window.onload) {window.onloadManager[0] = window.onload;}

//Loop through array and execute each on onload
window.onload = function() {
for (index in window.onloadManager) { window.onloadManager[index](); }

Adding functions to onloadmanager array.

window.onloadManager[window.onloadManager.length] = function()

alert('function1 added');


Say in other user control on same page we add one more function.

window.onloadManager[window.onloadManager.length] = function()

alert('function2 added');


Submit this story to DotNetKicks

1 comment:

Anonymous said...

nice article

Post a Comment

Post your comments/questions/feedback for this Article.


Latest Articles