Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add multiple window.onload events

In my ASP.NET User Control I'm adding some JavaScript to the window.onload event:

if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(), onloadScriptName))   Page.ClientScript.RegisterStartupScript(this.GetType(), onloadScriptName,      "window.onload = function() {myFunction();};", true);             

My problem is, if there is already something in the onload event, than this overwrites it. How would I go about allowing two user controls to each execute JavaScript in the onload event?

Edit: Thanks for the info on third party libraries. I'll keep them in mind.

like image 771
Ray Avatar asked Aug 13 '08 03:08

Ray


People also ask

Can you have multiple window onload?

Unfortunately, you cannot place multiple onload events on a single page. You can nest multiple functions within the one onload call, but what if you need to cascade an onload script across multiple pages, some which may already have an existing onload event? use the addLoadEvent function below.

Can you have multiple functions in one JavaScript file?

To import multiple functions from a module in JavaScript, specify an object with the function names you want to import. Notice how you do not need to use the . js extension in the file path. Also, remember you have to change the path from which to import if the module is located in another folder.

Does document onload and window onload fire at the same time?

The general idea is that window. onload fires when the document's window is ready for presentation and document. onload fires when the DOM tree (built from the markup code within the document) is completed.


1 Answers

Most of the "solutions" suggested are Microsoft-specific, or require bloated libraries. Here's one good way. This works with W3C-compliant browsers and with Microsoft IE.

if (window.addEventListener) // W3C standard {   window.addEventListener('load', myFunction, false); // NB **not** 'onload' }  else if (window.attachEvent) // Microsoft {   window.attachEvent('onload', myFunction); } 
like image 96
user83421 Avatar answered Sep 23 '22 21:09

user83421