5

IE has attribute readyState in document object, that indicates current state, e.g. "loading", "complete" etc.

Is there any way to find current loading state of document in Mozilla-based browsers? I'm aware of DOMContentLoaded event, but it wont fit my situation, as my code can be executed after this event was fired.

Added: no, I can't use any framework, and don't confuse with .readyState attribute of XHR object. And it's a bookmarklet, so it can be inserted in at any loading stage.

Added later: Anyway, it looks like it's not a big issue for me. Because this attribute will be added in FF3.6, and it does not break things badly in Firefox, when you manipulate on unfinished DOM (unlike IE).

4

2 回答 2

3

No, it's not possible. Sorry. But here's what you can do. If you can't test for stuff you want to be there before acting:

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

(This will definitely do it after the page renders, but it might be after onload, not after DOMContentLoaded.)

If you do know how to test for what you're looking for:

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

This will do it immediately, unless whatever you're looking for is not there, in which case it will wait until after the onload event.

Edit:

Check out this solution.

What it does is, in the edge cases, checks if the last element of document.getElementsByTagName("*") is undefined or not. And that seems to work for him, even in Opera.

于 2009-10-06T20:57:35.970 回答
2

可以执行吗?只需通知 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 );
于 2009-10-06T16:21:54.467 回答