基于 gecko 的浏览器的 document.readyState 模拟

发布于 2024-08-06 19:00:55 字数 547 浏览 12 评论 0原文

IE 在文档对象中有属性 readyState,该属性指示当前状态,例如“正在加载”、“完成”等。

在基于 Mozilla 的浏览器中,有没有办法找到文档的当前加载状态? 我知道 DOMContentLoaded 事件,但它不适合我的情况,因为我的代码可以在该事件触发后执行。

补充:不,我不能使用任何框架,并且不要与 XHR 对象的 .readyState 属性混淆。 它是一个书签,因此可以在任何加载阶段插入。

稍后补充: 无论如何,看起来这对我来说并不是什么大问题。因为这个属性将在FF3.6中添加,并且不会破坏当您操作未完成的 DOM 时,Firefox 中的情况会很糟糕(与 IE 不同)。

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悟红尘 2024-08-13 19:00:55

不,这是不可能的。对不起。但这是你可以做的。如果您无法在执行操作之前测试您想要的内容:(

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

这肯定会在页面呈现后执行,但可能是在加载之后,而不是在 DOMContentLoaded 之后。)

如果您确实知道如何测试您的内容re waiting for:

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

这将立即执行,除非您要查找的内容不存在,在这种情况下它将等到 onload 事件之后。

编辑:

查看此解决方案。

它的作用是,在边缘情况下,检查 document.getElementsByTagName("*") 的最后一个元素是否未定义。这似乎对他有用,即使在歌剧中也是如此。

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.

陪我终i 2024-08-13 19:00:55

可以执行吗?只需收到 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 );

Can be executed? Just be notified of the DOM event and store its state. I don't see what your root problem is. Surely you could rip out the guts of this method and adapt it to your situation.

jQuery's way of doing it:

// 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 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文