Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding to window.onload event?

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?

like image 469
pepper Avatar asked Mar 22 '13 06:03

pepper


People also ask

How do you add onload event to a div element?

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.

Can you have two window onload functions?

You can not bind several functions to window. onload and expect all of these functions will be executed.

Is window onload an event?

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.


2 Answers

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); } 
like image 23
karaxuna Avatar answered Sep 19 '22 14:09

karaxuna


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) 
like image 77
John Dvorak Avatar answered Sep 19 '22 14:09

John Dvorak