Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

javascript - $(document).ready等同于没有jQuery($(document).ready equivalent without jQuery)

I have a script that uses $(document).ready , but it doesn't use anything else from jQuery.

(我有一个使用$(document).ready的脚本,但是它不使用jQuery的其他任何东西。)

I'd like to lighten it up by removing the jQuery dependency.

(我想通过删除jQuery依赖项来减轻它的负担。)

How can I implement my own $(document).ready functionality without using jQuery?

(如何在不使用jQuery的情况下实现自己的$(document).ready功能?)

I know that using window.onload will not be the same, as window.onload fires after all images, frames, etc. have been loaded.

(我知道使用window.onload会有所不同,因为在加载所有图像,框架等之后, window.onload会触发。)

  ask by FlySwat translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is a standards based replacement, DOMContentLoaded that is supported by over 98% of browsers , though not IE8:

(有超过98%的浏览器都支持基于标准的替换DOMContentLoaded ,尽管IE8不支持:)

document.addEventListener("DOMContentLoaded", function(event) { 
  //do work
});

jQuery's native function is much more complicated than just window.onload, as depicted below.

(jQuery的本机功能比window.onload复杂得多,如下所示。)

function bindReady(){
    if ( readyBound ) return;
    readyBound = true;

    // Mozilla, Opera and webkit nightlies currently support this event
    if ( document.addEventListener ) {
        // Use the handy event callback
        document.addEventListener( "DOMContentLoaded", function(){
            document.removeEventListener( "DOMContentLoaded", arguments.callee, false );
            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", function(){
            if ( document.readyState === "complete" ) {
                document.detachEvent( "onreadystatechange", arguments.callee );
                jQuery.ready();
            }
        });

        // If IE and not an iframe
        // continually check to see if the document is ready
        if ( document.documentElement.doScroll && window == window.top ) (function(){
            if ( jQuery.isReady ) return;

            try {
                // If IE is used, use the trick by Diego Perini
                // http://javascript.nwbox.com/IEContentLoaded/
                document.documentElement.doScroll("left");
            } catch( error ) {
                setTimeout( arguments.callee, 0 );
                return;
            }

            // and execute any waiting functions
            jQuery.ready();
        })();
    }

    // A fallback to window.onload, that will always work
    jQuery.event.add( window, "load", jQuery.ready );
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...