Question :
Add multiple window.onload events,
Answer :
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.
,
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); }
That’s the answer Add multiple window.onload events, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.
Disclaimer :
The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you