简体   繁体   English

Firefox DOMContentLoaded事件问题

[英]Firefox DOMContentLoaded event problem

i've created a script that works with the Dom so it has to wait until the Dom is ready before execute every operation. 我创建了一个与Dom一起使用的脚本,因此它必须等到Dom准备好后才能执行每个操作。 I want that this script can be included in two ways: 我希望可以通过两种方式包含此脚本:

  • In the head tag so that it is loaded before the Dom is ready. 在head标签中,以便在准备好Dom之前将其加载。 I've used document.addEventListener("DOMContentLoaded",function(){...},false) for this and it works well 我为此使用了document.addEventListener("DOMContentLoaded",function(){...},false) ,它运行良好
  • Loaded dinamically when the Dom is already loaded. 如果已加载Dom,则以动态方式加载。 In this case the script doesn't work because for some reasons if the Dom is already loaded the DOMContentLoaded event isn't fired 在这种情况下,该脚本将无法运行,因为由于某些原因,如果Dom已加载,则不会触发DOMContentLoaded事件。

So i want to know, is there a way to check if the Dom is already loaded so that the script can execute the code without using the DOMContentLoaded event? 所以我想知道,有没有一种方法可以检查Dom是否已经加载,以便脚本可以执行代码而无需使用DOMContentLoaded事件?

PS: this must be an external script so i have no control on the page where it will be included PS:这必须是外部脚本,所以我无法控制要包含它的页面

Ok, this is cheesy but it should work: Wrap your external script in an init() method: 好的,这很俗气,但应该可以:将外部脚本包装在init()方法中:

eg (and I'm just guessing here) 例如(我只是在这里猜测)

var myModule = {

    init: function {
        //all the code goes here
        /..
    }
 }

Then put your script import back into the head tag and at the very bottom of your DOM markup add a script tag 然后将您的脚本导入回到head标签中,并在DOM标记的最底部添加一个脚本标签

<script>
    myModule.init();
</script>

Again I wouldn't usually recommend it but if you need to avoid checking the DOM loaded event that's what I'd do 同样,我通常不推荐这样做,但是如果您需要避免检查DOM加载事件,那就是我要做的

One of the easiest ways to do it would be to check if you can getElementById for something at the end of the page. 最简单的方法之一是检查是否可以在页面末尾获取getElementById。 Something like: 就像是:

if (document.getElementById('footer')) { /* Dom is probably loaded */ }
else { /* Dom is probably NOT loaded */ }

But really, the best practice is probably to make sure that the code is always being called the same way. 但实际上,最佳实践可能是确保始终以相同的方式调用代码。 you could use an 'onload' event instead, as that will work in all browsers and is usually only a fraction of a second after the DOMContentLoaded event. 您可以改用“ onload”事件,因为该事件在所有浏览器中都有效,并且通常仅在DOMContentLoaded事件之后的一秒钟之内。 But, I suppose that's really up to you. 但是,我认为那完全取决于您。

Define your function: function domReadyHandler() { /* ... */ } 定义函数: function domReadyHandler() { /* ... */ }

Add the event listener: document.addEventListener('DOMContentLoaded', domReadyHandler, false); 添加事件侦听器: document.addEventListener('DOMContentLoaded', domReadyHandler, false);

Execute the handler after dynamically loading your content, too: function myXhrCallback() { /* ... */; domReadyHandler(); } 动态加载内容后也执行处理程序: function myXhrCallback() { /* ... */; domReadyHandler(); } function myXhrCallback() { /* ... */; domReadyHandler(); }

You could set a global var saying that the script is dynamically being included, Depending on this you can fire the load listener when the script is being eval'd. 您可以设置一个全局变量,说该脚本是动态包含的,据此,您可以在评估脚本时触发负载侦听器。

window.SCRIPT_IS_DYNAMICALLY_LOADED = true;
< include script>

In script: 在脚本中:

if (window.SCRIPT_IS_DYNAMICALLY_LOADED) {
     domContentLoadedListener();
} else {
     document.addEventListener("DOMContentLoaded",domContentLoadedListener, false);
}

Check the value for document.readyState. 检查document.readyState的值。 If the value is 'complete', you can execute the script, if it's something else, add the listener. 如果值为“ complete”,则可以执行脚本;如果值为其他值,则添加侦听器。

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

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