jQuery(document).ready多久会被调用一次? [英] How soon will jQuery(document).ready be called?

查看:114
本文介绍了jQuery(document).ready多久会被调用一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果第三方javascript文件挂起并需要一段时间才能加载,则将

If a third party javascript file hangs and takes a while to load, will

jQuery(document).ready(function() {}) 

必须等待加载之后才能被调用吗?

have to wait for that to load before being called?

推荐答案

是的,它必须等待. 特别是,您不能依靠jQuery(document).ready()触发其他脚本有机会执行之前. ready绑定到DOMContentReady,readystatechanged或onload(无论哪个可用).

Yes, it has to wait. In particular, you cannot rely on jQuery(document).ready() to fire before other scripts get a chance to execute. ready binds to DOMContentReady, readystatechanged, or onload, whichever is available.

文档指出在大多数情况下,可以在完全构建DOM层次结构后立即运行脚本" .请注意,仅保证当此事件触发时DOM已准备就绪. 它不能保证您有其他任何事情,因为它不能.

The documentation states that "in most cases, the script can be run as soon as the DOM hierarchy has been fully constructed". Note that the only guarantee is that the DOM is ready when this event fires. It does not guarantee you anything else - because it just cannot.

例如,这不适用于IE,Firefox或Chromium, brilliant.js ready()处理程序有机会执行之前,始终被称为 您如何改组脚本标签:

This, for example will not work in IE, Firefox or Chromium, brilliant.js is always called before the ready() handler has a chance to execute no matter how you shuffle the script tags:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Test</title>
    <script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.js" charset="utf-8" type="text/javascript" ></script>
</head>
<body>
    <script type="text/javascript" >
    // <![CDATA[
        alert("attaching event");
        $(document).ready(function () { alert("fired"); });
    // ]]> 
    </script>
    <script type="text/javascript" src="brilliant.js" ></script>
</body>
</html>

仅供参考,这是jquery-1.4.2中的相关代码:

FYI, here is the relevant code from jquery-1.4.2:

bindReady: function() {
    if ( readyBound ) {
        return;
    }

    readyBound = true;

    // Catch cases where $(document).ready() is called after the
    // browser event has already occurred.
    if ( document.readyState === "complete" ) {
        return jQuery.ready();
    }

    // Mozilla, Opera and webkit nightlies currently support this event
    if ( document.addEventListener ) {
        // Use the handy event callback
        document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );

        // A fallback to window.onload, that will always work
        window.addEventListener( "load", jQuery.ready, false );

    // If IE event model is used
    } else if ( document.attachEvent ) {
        // ensure firing before onload,
        // maybe late but safe also for iframes
        document.attachEvent("onreadystatechange", DOMContentLoaded);

        // A fallback to window.onload, that will always work
        window.attachEvent( "onload", jQuery.ready );

        // If IE and not a frame
        // continually check to see if the document is ready
        var toplevel = false;

        try {
            toplevel = window.frameElement == null;
        } catch(e) {}

        if ( document.documentElement.doScroll && toplevel ) {
            doScrollCheck();
        }
    }
},

这篇关于jQuery(document).ready多久会被调用一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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