Javascript DOMContentLoaded 事件未在 Internet Explorer 中触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24904010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript DOMContentLoaded event not firing in Internet Explorer
提问by Cedric Krause
I have the following code to attach a function to the DOMContentLoaded
event, but the function is never called in Internet Explorer 11
我有以下代码可将函数附加到DOMContentLoaded
事件,但从未在 Internet Explorer 11 中调用该函数
Code:
代码:
if (document.addEventListener) {
document.addEventListener("DOMContentLoaded", init, false);
}
else {
document.attachEvent("onDOMContentLoaded", init);
}
回答by jfriend00
There are several reasons it might not fire:
它可能不会触发有几个原因:
- It has already fired (before you attached the event handler) and you missed it.
- You're running an older version of IE that doesn't support the
DOMContentLoaded
event. - There's some sort of script error before these lines of code so these lines of code are not actually executed and thus the event handler is never actually registered.
- You're trying to do this on an embedded iFrame and may not have the correct document for the iFrame (some browsers can switch the document when loading external source).
- 它已经触发(在您附加事件处理程序之前)并且您错过了它。
- 您正在运行不支持该
DOMContentLoaded
事件的旧版 IE 。 - 在这些代码行之前存在某种脚本错误,因此这些代码行实际上并未执行,因此事件处理程序从未实际注册过。
- 您正在尝试在嵌入式 iFrame 上执行此操作,并且可能没有正确的 iFrame 文档(某些浏览器可以在加载外部源时切换文档)。
To check for script errors, you should open the debug console in IE (press F12) and look at the console to see if any script errors are being reported.
要检查脚本错误,您应该在 IE 中打开调试控制台(按 F12)并查看控制台以查看是否报告了任何脚本错误。
You can check to see if document.readyState === "complete"
to see if it has already fired.
您可以检查document.readyState === "complete"
它是否已经触发。
And, in versions of IE before IE9 where you would need attachEvent
, IE doesn't support DOMContentLoaded
so your else branch would not work. You would have to use different detection methods in those older versions of IE.
而且,在您需要的 IE9 之前的 IE 版本中attachEvent
,IE 不支持,DOMContentLoaded
因此您的 else 分支将无法工作。您将不得不在那些旧版本的 IE 中使用不同的检测方法。
You can see a well tested, cross-browser, plain javascript function for getting notified when the document is ready here: pure JavaScript equivalent to jQuery's $.ready() how to call a function when the page/dom is ready for it.
您可以在这里看到一个经过良好测试的跨浏览器纯 javascript 函数,用于在文档准备好时获得通知:纯 JavaScript 相当于 jQuery 的 $.ready() 如何在页面/dom 准备好时调用函数。
回答by gkweb
To break this down with a workable example see below. The issue I found was that DOMContentLoaded
won't fire for javascript
unless the event is created inline within the document itself.
用一个可行的例子来分解这个,见下文。我发现的问题是,除非事件是在文档本身内内联创建的DOMContentLoaded
,javascript
否则不会触发。
A simple solution for this is to add a check to the document readyState
. If it's still loading - create the event because it's possible for DOMContentLoaded
to fire - Otherwise just load immediately because DOM is ready.
对此的一个简单解决方案是向文档添加检查readyState
。如果它仍在加载 - 创建事件,因为它可能DOMContentLoaded
触发 - 否则立即加载,因为 DOM 已准备就绪。
var load = function () {
console.log('I will always load, woohoo');
};
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', load); // Document still loading so DomContentLoaded can still fire :)
} else {
load();
}