Javascript - 如何检测文档是否已加载(IE 7/Firefox 3)
我想在文档加载后调用函数,但文档可能尚未完成加载,也可能尚未完成。 如果它确实加载了,那么我就可以调用该函数。 如果它没有加载,那么我可以附加一个事件监听器。 在 onload 已经触发后,我无法添加事件监听器,因为它不会被调用。 那么如何检查文档是否已加载呢? 我尝试了下面的代码,但它并不完全有效。 有任何想法吗?
var body = document.getElementsByTagName('BODY')[0];
// CONDITION DOES NOT WORK
if (body && body.readyState == 'loaded') {
DoStuffFunction();
} else {
// CODE BELOW WORKS
if (window.addEventListener) {
window.addEventListener('load', DoStuffFunction, false);
} else {
window.attachEvent('onload', DoStuffFunction);
}
}
I want to call a function after a document loads, but the document may or may not have finished loading yet. If it did load, then I can just call the function. If it did NOT load, then I can attach an event listener. I can't add an eventlistener after onload has already fired since it won't get called. So how can I check if the document has loaded? I tried the code below but it doesn't entirely work. Any ideas?
var body = document.getElementsByTagName('BODY')[0];
// CONDITION DOES NOT WORK
if (body && body.readyState == 'loaded') {
DoStuffFunction();
} else {
// CODE BELOW WORKS
if (window.addEventListener) {
window.addEventListener('load', DoStuffFunction, false);
} else {
window.attachEvent('onload', DoStuffFunction);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
不需要 galambalazs 提到的所有代码。 在纯 JavaScript 中执行此操作的跨浏览器方法就是简单地测试
document.readyState
:这也是 jQuery 的做法。
根据 JavaScript 的加载位置,这可以在一个时间间隔内完成:
事实上,
document.readyState
可以具有三种状态:因此,如果您只需要 DOM准备好了,检查
document.readyState === "interactive"
。 如果您需要准备好整个页面(包括图像),请检查document.readyState ===“complete”
。There's no need for all the code mentioned by galambalazs. The cross-browser way to do it in pure JavaScript is simply to test
document.readyState
:This is also how jQuery does it.
Depending on where the JavaScript is loaded, this can be done inside an interval:
In fact,
document.readyState
can have three states:So if you only need the DOM to be ready, check for
document.readyState === "interactive"
. If you need the whole page to be ready, including images, check fordocument.readyState === "complete"
.不需要图书馆。 顺便说一句,jQuery 使用这个脚本有一段时间了。
http://dean.edwards.name/weblog/2006/06/again/
No need for a library. jQuery used this script for a while, btw.
http://dean.edwards.name/weblog/2006/06/again/
您可能想使用 jQuery 之类的东西,它使 JS 编程更容易。
比如:
似乎会做你想做的事。
You probably want to use something like jQuery, which makes JS programming easier.
Something like:
Would seem to do what you are after.
如果您确实希望此代码在 load 时运行,而不是在 domready 时运行(即您还需要加载图像),那么不幸的是,ready 函数无法为您执行此操作。 我通常只是做这样的事情:
包含在文档javascript中(即总是在onload触发之前调用):
然后你的代码:(
或者你的偏好框架中的等效代码。)我使用此代码为未来页面预先缓存javascript和图像。 由于我获得的内容根本不用于此页面,因此我不希望它优先于图像的快速下载。
也许有更好的方法,但我还没有找到。
If you actually want this code to run at load, not at domready (ie you need the images to be loaded as well), then unfortunately the ready function doesn't do it for you. I generally just do something like this:
Include in document javascript (ie always called before onload fired):
Then your code:
(Or the equivalent in your framework of preference.) I use this code to do precaching of javascript and images for future pages. Since the stuff I'm getting isn't used for this page at all, I don't want it to take precedence over the speedy download of images.
There may be a better way, but I've yet to find it.
Mozila Firefox 表示
onreadystatechange
是DOMContentLoaded
的替代方案。在
DOMContentLoaded
中,Mozila 的文档说:我认为
load
事件应该用于完整的文档+资源加载。Mozila Firefox says that
onreadystatechange
is an alternative toDOMContentLoaded
.In
DOMContentLoaded
the Mozila's doc says:I think
load
event should be used for a full document+resources loading.上面使用 JQuery 的方法是最简单也是最常用的方法。 但是,您可以使用纯 javascript,但尝试在头部定义此脚本,以便在开始时读取它。 您正在寻找的是
window.onload
事件。下面是我创建的一个用于运行计数器的简单脚本。 计数器在 10 次迭代后停止
The above one with JQuery is the easiest and mostly used way. However you can use pure javascript but try to define this script in the head so that it is read at the beginning. What you are looking for is
window.onload
event.Below is a simple script that I created to run a counter. The counter then stops after 10 iterations
尝试这个:
Try this:
我有其他解决方案,我的应用程序需要在创建 MyApp 的新对象时启动,所以它看起来像:
据我所知,它适用于所有浏览器。
I have other solution, my application need to be started when new object of MyApp is created, so it looks like:
it is working on all browsers, that i know.