基于 gecko 的浏览器的 document.readyState 模拟
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,这是不可能的。对不起。但这是你可以做的。如果您无法在执行操作之前测试您想要的内容:(
这肯定会在页面呈现后执行,但可能是在加载之后,而不是在 DOMContentLoaded 之后。)
如果您确实知道如何测试您的内容re waiting for:
这将立即执行,除非您要查找的内容不存在,在这种情况下它将等到 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:
(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:
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.
可以执行吗?只需收到 DOM 事件的通知并存储其状态即可。我不明白你的根本问题是什么。当然,您可以去掉这种方法的核心内容,并根据您的情况进行调整。
jQuery 的实现方式:
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: