I'm wondering how to add another method call to the window.onload event once it has already been assigned a method call.
Suppose somewhere in the script I have this assignment...
window.onload = function(){ some_methods_1() };
and then later on in the script I have this assignment
window.onload = function(){ some_methods_2() };
As it stands, only some_methods_2
will be called. Is there any way to add to the previous window.onload
callback without cancelling some_methods_1
? (and also without including both some_methods_1()
and some_methods_2()
in the same function block).
I guess this question is not really about window.onload
but a question about javascript in general. I DON'T want to assign something to window.onload
in such a way that that if another developer were to work on the script and add a piece of code that also uses window.onload
(without looking at my previous code), he would disable my onload event.
I'm also wondering the same thing about
$(document).ready()
in jquery. How can I add to it without destroying what came before, or what might come after?
The onload event can only be used on the document(body) itself, frames, images, and scripts. In other words, it can be attached to only body and/or each external resource. The div is not an external resource and it's loaded as part of the body, so the onload event doesn't apply there.
You can not bind several functions to window. onload and expect all of these functions will be executed.
JavaScript has a window onload event to launch certain functions whenever a web page is loaded. The onload event is also used for verification of type and version of visitor's browser. Further cookies can also be checked through the onload attribute. The attribute of onload triggers when the object is loaded in HTML.
You can use attachEvent(ie8) and addEventListener instead
addEvent(window, 'load', function(){ some_methods_1() }); addEvent(window, 'load', function(){ some_methods_2() }); function addEvent(element, eventName, fn) { if (element.addEventListener) element.addEventListener(eventName, fn, false); else if (element.attachEvent) element.attachEvent('on' + eventName, fn); }
If you are using jQuery, you don't have to do anything special. Handlers added via $(document).ready()
don't overwrite each other, but rather execute in turn:
$(document).ready(func1) ... $(document).ready(func2)
If you are not using jQuery, you could use addEventListener
, as demonstrated by Karaxuna, plus attachEvent
for IE<9.
Note that onload
is not equivalent to $(document).ready()
- the former waits for CSS, images... as well, while the latter waits for the DOM tree only. Modern browsers (and IE since IE9) support the DOMContentLoaded
event on the document, which corresponds to the jQuery ready
event, but IE<9 does not.
if(window.addEventListener){ window.addEventListener('load', func1) }else{ window.attachEvent('onload', func1) } ... if(window.addEventListener){ window.addEventListener('load', func2) }else{ window.attachEvent('onload', func2) }
If neither option is available (for example, you are not dealing with DOM nodes), you can still do this (I am using onload
as an example, but other options are available for onload
):
var oldOnload1=window.onload; window.onload=function(){ oldOnload1 && oldOnload1(); func1(); } ... var oldOnload2=window.onload; window.onload=function(){ oldOnload2 && oldOnload2(); func2(); }
or, to avoid polluting the global namespace (and likely encountering namespace collisions), using the import/export IIFE pattern:
window.onload=(function(oldLoad){ return function(){ oldLoad && oldLoad(); func1(); } })(window.onload) ... window.onload=(function(oldLoad){ return function(){ oldLoad && oldLoad(); func2(); } })(window.onload)
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