简体   繁体   English

如何在IE中的文档上触发“onload”事件

[英]How to fire “onload” event on document in IE

I am currently developing Unit Tests for a Javascript method that detects the readiness of the document. 我目前正在为Javascript方法开发单元测试,以检测文档的准备情况。 This code is already at framework level, so please avoid mentions of this being already implemented in jQuery or another library. 此代码已经处于框架级别,因此请避免在jQuery或其他库中实现此代码。

I have successfully simulated the 'readystatechange' change event with the following code: 我已使用以下代码成功模拟了'readystatechange'更改事件:

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

I failed to do the same for the 'load' event. 我没有为'load'事件做同样的事情。 The following code results in an invalid argument error in IE7, thrown by the call to fireEvent on the last line: 以下代码导致IE7中的无效参数错误,在最后一行调用fireEvent抛出:

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: 编辑:按照Crescent Fresh的建议,我将代码更改为:

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

There is no more error, but the listener for 'onload' does not fire. 没有更多的错误,但'onload'的监听器不会触发。 Here is how I configured it: 这是我配置它的方式:

document.attachEvent('onload',listener);

According to this page at MSDN , there's no onload event for document . 根据MSDN的这个页面, document没有onload事件。

You want either window.onload or document.body.onload . 你想要window.onloaddocument.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 . 这些在IE中是相同的:由于历史原因, <body onload="...">实际上设置了window.onload ,因此MS决定使document.body.onload成为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. 这个问题是 - 正如Eric在评论中提到的那样 - 似乎没有办法手动触发窗口事件,这意味着可能没有针对Eric问题的解决方案。

For some reason, it appears that IE overrides the onload property of window with an empty object after the DOM is loaded. 出于某种原因,在加载DOM之后,IE似乎会覆盖带有空对象的windowonload属性。 At least that is the case when you try to access it from within any event handler of a DOM element... 至少在尝试从DOM元素的任何事件处理程序中访问它时就是这种情况......

<!DOCTYPE html>
<html>
  <head>
    <title>Test by Josh</title>
    <script type="text/javascript">
      window.onload = function() {
        alert("Test");
      }
      alert(typeof window.onload);
    </script>
  </head>
  <body>
    <h1 onclick="alert(typeof window.onload);">Test</h1>
  </body>
</html>

In this situation, you'll see that window.onload is recognized as a function initially, then you see the "Test" alert. 在这种情况下,您将看到window.onload最初被识别为函数,然后您会看到“测试”警报。 When you click on the heading, you'll see that window.onload is now an object . 当您单击标题时,您将看到window.onload现在是一个object I tried iterating through the properties of the object, but it's empty. 我尝试迭代对象的属性,但它是空的。 This is not cool. 这不酷。

One lame workaround is to grab the function in the accessible scope and assign it to a different property that you can fire at your convenience... 一个蹩脚的解决方法是在可访问范围内获取该功能并将其分配给您可以在方便时触发的其他属性...

<!DOCTYPE html>
<html>
  <head>
    <title>Test by Josh</title>
    <script type="text/javascript">
      window.onload = function() {
        alert("Test");
      }         
    </script>
  </head>
  <body>
    <h1 onclick="window.onloadfix()">Test</h1>
    <!-- Could potentially be injected via server-side include if needed -->
    <script type="text/javascript">
      window.onloadfix = function() {
        window.onload();
      }
    </script>
  </body>
</html>

I can't think of any other way to address this issue right now. 我现在想不出任何其他方式来解决这个问题。

The load event will fire when the document (including external resources such as images) has fully loaded, and not before. 当文档(包括图像等外部资源)已完全加载时,而不是之前,将触发加载事件。

What results are you getting from your attempts to fire readystatechange ? 你试图解雇readystatechange会得到什么结果? Does the readyState value actually change at all? readyState值是否实际发生了变化? Whether or not, that's not of much use either: either you fire the event with a readyState that hasn't changed, or you do so with a readyState that isn't a valid reflection of the state of the document. 是否,这也没有多大用处:要么使用未更改的readyState来触发事件,要么使用不是文档状态的有效反映的readyState来执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM