window.onload似乎在DOM加载之前触发(JavaScript) [英] window.onload seems to trigger before the DOM is loaded (JavaScript)

查看:424
本文介绍了window.onload似乎在DOM加载之前触发(JavaScript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在window.onload和document.onload事件中遇到麻烦。我看到的一切告诉我,直到DOM完全加载所有的资源,这些不会触发,似乎这对我来说没有发生:

I am having trouble with the window.onload and document.onload events. Everything I read tells me these will not trigger until the DOM is fully loaded with all its resources, it seems like this isn't happening for me:

我尝试了以下Chrome 4.1.249.1036(41514)和IE 8.0.7600.16385中的简单页面具有相同的结果:两者都显示消息It failed!,表示myParagraph未加载(因此DOM似乎不完整)。

I tried the following simple page in Chrome 4.1.249.1036 (41514) and IE 8.0.7600.16385 with the same result: both displayed the message "It failed!", indicating that myParagraph is not loaded (and so the DOM seems incomplete).

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <script type="text/javascript">
            window.onload = doThis();
            // document.onload gives the same result

            function doThis() {
                if (document.getElementById("myParagraph")) {
                    alert("It worked!");
                } else {
                    alert("It failed!");
                }
            }
        </script>
    </head>

    <body>
        <p id="myParagraph">Nothing is here.</p>
    </body>
</html>

我在外部的.js文件中使用比这更复杂的脚本,但这说明了问题。我可以通过window.onload设置一个计时器半秒钟来运行doThis(),但这似乎是一个不成熟的解决方案,并没有回答为什么window.onload似乎没有做什么大家都说这样。另一个解决方案是设置一个定时器,它将检查DOM是否被加载,如果不是,它将会在半秒钟后自动调用(因此它将继续检查,直到DOM加载),但这似乎对我来说过于复杂。
是否有更合适的事件?

I am using more complex scripts than this, in an external .js file, but this illustrates the problem. I can get it working by having window.onload set a timer for half a second to run doThis(), but this seems like an inelegant solution, and doesn't answer the question of why window.onload doesn't seem to do what everyone says it does. Another solution would be to set a timer that will check if the DOM is loaded, and if not it will just call itself half a second later (so it will keep checking until the DOM is loaded), but this seems overly complex to me. Is there a more appropriate event to use?

推荐答案

在加载窗口时,身体不是因此,您应该按照以下方式更正您的代码:

At the time window is loaded the body isn't still loaded therefore you should correct your code in the following manner:

<script type="text/javascript">
    window.onload = function(){
        window.document.body.onload = doThis; // note removed parentheses
    };

    function doThis() {
        if (document.getElementById("myParagraph")) {
            alert("It worked!");
        } else {
            alert("It failed!");
        }
    }
</script>

测试工作在FF / IE / Chrome中,虽然考虑处理文档

Tested to work in FF/IE/Chrome, although thinking about handling document.onload too.

如上所述,使用js-framework将是一个更好的主意。

As already mentioned, using js-frameworks will be a far better idea.

这篇关于window.onload似乎在DOM加载之前触发(JavaScript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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