Document.readyState analog for gecko-based browsers

IE has a readyState attribute in a document object that indicates the current state, for example. "download", "full", etc.

Is there a way to find the current state of document loading in Mozilla-based browsers? I am aware of the DOMContentLoaded event, but it does not correspond to my situation, since my code can be run after this event.

Added: no, I cannot use any framework and am not confused with the .readyState attribute of an XHR object. And this is a bookmarklet, so it can be inserted at any stage of loading.

Added later: in any case, it seems like this is not a big problem for me. Since this attribute will be added in FF3.6 , and it will not greatly disrupt the situation in firefox when you manipulate the incomplete DOM ( unlike IE ).

+5
source share
2 answers

No, It is Immpossible. I'm sorry. But here is what you can do. If you cannot test the material you want to be there before you act:

window.setTimeout(function () {
    // do your stuff here
}, 0);

(This will surely do this after the page is displayed, but it may be after loading, and not after DOMContentLoaded.)

If you know how to check what you are looking for:

(function () {
    if (/* test if what you're looking for is there */) {
        // do your stuff
    } else {
        window.setTimeout(arguments.callee, 0);
    }
})();

, , , , onload.

Edit:

.

, , , , document.getElementsByTagName( "*" ) undefined . , , , Opera.

+3

? DOM . , . , .

jQuery:

// Mozilla, Opera and webkit nightlies currently support this event
if ( document.addEventListener ) {
    // Use the handy event callback
    document.addEventListener( "DOMContentLoaded", function(){
              //do stuff
    }, 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 );
+2

All Articles