admin管理员组文章数量:1201993
I have the following code to attach a function to the DOMContentLoaded
event, but the function is never called in Internet Explorer 11
Code:
if (document.addEventListener) {
document.addEventListener("DOMContentLoaded", init, false);
}
else {
document.attachEvent("onDOMContentLoaded", init);
}
I have the following code to attach a function to the DOMContentLoaded
event, but the function is never called in Internet Explorer 11
Code:
if (document.addEventListener) {
document.addEventListener("DOMContentLoaded", init, false);
}
else {
document.attachEvent("onDOMContentLoaded", init);
}
Share
Improve this question
edited Feb 9, 2021 at 9:46
Brian Tompsett - 汤莱恩
5,88372 gold badges61 silver badges133 bronze badges
asked Jul 23, 2014 at 7:13
Cedric KrauseCedric Krause
3811 gold badge6 silver badges17 bronze badges
1
- Can you specify where you are calling this code, any on document load or button click!?? Please create a jsFiddle so that we can help better. – MarmiK Commented Jul 23, 2014 at 7:35
2 Answers
Reset to default 14There 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).
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.
You can check to see if document.readyState === "complete"
to see if it has already fired.
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.
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.
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.
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.
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();
}
本文标签: Javascript DOMContentLoaded event not firing in Internet ExplorerStack Overflow
版权声明:本文标题:Javascript DOMContentLoaded event not firing in Internet Explorer - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738641176a2104318.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论