如何发射“上载”事件在IE上的文档 [英] How to fire "onload" event on document in IE

查看:133
本文介绍了如何发射“上载”事件在IE上的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发用于检测文档准备情况的JavaScript方法的单元测试。这个代码已经在框架级别,所以请避免提到这个已经在jQuery或其他库中实现了。



我已经成功地模拟了'readystatechange'以下代码:

  var event; 
event = document.createEventObject();
event.type ='readystatechange';
document.fireEvent('onreadystatechange',event);

我没有为'load'事件做同样的事情。以下代码导致IE7中的无效参数错误,在最后一行调用fireEvent引发:

  event = document .createEventObject(); 
event.type ='load';
document.fireEvent('onload',event);

有人做过这个,还是以前没有这样做?我也有兴趣以不同的方式来发起活动。



编辑:按照Crescent Fresh的建议,我改变了我的代码:

  event = document.createEventObject(); 
event.type ='load';
document.body.fireEvent('onload',event);

没有更多错误,但'onload'的侦听器不会触发。这是我如何配置它:

  document.attachEvent('onload',listener); 


解决方案

根据此页面在MSDN ,没有 onload document.body.onload 。这些在IE中是相同的:由于历史原因,< body onload =...> 实际设置 window.onload ,所以MS决定将 document.body.onload 别名为 window.onload



这个问题是 - 正如Eric在评论中提到的那样,似乎没有办法手动触发窗口事件,这意味着可能没有解决方案对于埃里克的问题。


I am currently developing Unit Tests for a Javascript method that detects the readiness of the document. This code is already at framework level, so please avoid mentions of this being already implemented in jQuery or another library.

I have successfully simulated the 'readystatechange' change event with the following code:

var event;
event = document.createEventObject();
event.type = 'readystatechange';
document.fireEvent('onreadystatechange',event);

I failed to do the same for the 'load' event. The following code results in an invalid argument error in IE7, thrown by the call to fireEvent on the last line:

event = document.createEventObject();
event.type = 'load';
document.fireEvent('onload',event);

Has anyone done this, or failed to do this before? I am also interested in any suggestion to fire the event in a different way.

Edit: following the suggestion by Crescent Fresh, I changed my code to:

event = document.createEventObject();
event.type = 'load';
document.body.fireEvent('onload',event);

There is no more error, but the listener for 'onload' does not fire. Here is how I configured it:

document.attachEvent('onload',listener);

解决方案

According to this page at MSDN, there's no onload event for document.

You want either window.onload or document.body.onload. These are identical in IE: for historical reasons, <body onload="..."> actually sets window.onload, so MS decided to make document.body.onload an alias of window.onload.

The problem with this is - as Eric mentioned in the comments - that there doesn't seem to be a way to manually fire window events, which means that there might not be a solution for Eric's problem.

这篇关于如何发射“上载”事件在IE上的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆