adding to window.onload event?

http://stackoverflow.com/questions/15564029/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 previouswindow.onload callback without cancelling some_methods_1 ? (and also without including bothsome_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 towindow.onload in such a way that that if another developer were to work on the script and add a piece of code that also useswindow.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?

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, plusattachEvent 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.

if(document.addEventListener){
  document.addEventListener('load', func1)
}else{
  document.attachEvent('load', func1)
}
...
if(document.addEventListener){
  document.addEventListener('load', func2)
}else{
  document.attachEvent('load', func2)
}

If neither option is available (for example, you are not dealing with DOM nodes), you can still do this (I am usingonload as an example, but other options are available for onload):

var oldOnload1=document.onload;
document.onLoad1=function(){
  oldOnload1 && oldOnload1();
  func1();
}
...
var oldOnload2=document.onload;
document.onLoad2=function(){
  oldOnload2 && oldOnload2();
  func2();
}

or, to avoid polluting the global namespace (and encountering likely collisions), using the import/export IIFE pattern:

document.onLoad=(function(onLoad){
  return function(){
    oldLoad && onLoad();
    func1();
  }
})(document.onLoad)
...
document.onLoad=(function(onLoad){
  return function(){
    oldLoad && onLoad();
    func2();
  }
})(document.onLoad)
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值