How do I add an additional window.onload event in Javascript?

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.

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); }.

Very usefu, thank you! – Andrea Di Persio May 11 '09 at 13:41 Note: function assigned(myFunction) in window. AttachEvent('onload', myFunction); is always fired after any function assigned to window.

Onload – user961954 Oct 17 at 22:02.

There still is an ugly solution (which is far inferior to using a framework or addEventListener/attachEvent) that is to save the current onload event: function addOnLoad(fn) { var old = window. Onload; window. Onload = function() { old(); fn(); }; } addOnLoad(function() { // your code here }); addOnLoad(function() { // your code here }); addOnLoad(function() { // your code here }); Note that frameworks like jQuery will provide a way to execute code when the DOM is ready and not when the page loads.

DOM being ready means that your HTML has loaded but not external components like images or stylesheets, allowing you to be called long before the load event fires.

If you use jQuery you can have multiple $(document). Ready(function(){}); which is in essence a window. Onload event.

Try this: window. AttachEvent("onload", myOtherFunctionToCall); function myOtherFunctionToCall() { // do something } edit: hey, I was just getting ready to log in with Firefox and reformat this myself! Still doesn't seem to format code for me with IE7.

Mootools is another great JavaScript framework which is fairly easy to use, and like RedWolves said with jQuery you can can just keep chucking as many handlers as you want. For every *. Js file I include I just wrap the code in a function.Window.

AddEvent('domready', function(){ alert('Just put all your code here'); }); And there are also advantages of using domready instead of onload.

I don't know a lot about ASP. NET, but why not write a custom function for the onload event that in turn calls both functions for you? If you've got two functions, call them both from a third script which you register for the event.

Actually, according to this MSDN page, it looks like you can call this function multiple times to register multiple scripts. You just need to use different keys (the second argument). Page.ClientScript.

RegisterStartupScript( this.GetType(), key1, function1, true); Page.ClientScript. RegisterStartupScript( this.GetType(), key2, function2, true); I believe that should work.

I know this has been answered, but I just want you to know about this function: phrogz.net/JS/AttachEvent_js.txt regards T.

I had a similar problem today so I solved it having an index. Js with the following: window. OnloadFuncs = ; window.

Onload = function() { for(var I in this. OnloadFuncs) { this.onloadFuncsi(); } } and in additional js files that I want to attach the onload event I just have to do this: window.onloadFuncs. Push(function(){ // code here }); I normally use jQuery though, but this time I was restricted to pure js wich forced to use my mind for a while!

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions