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.
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.
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.
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.
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With