问题描述
我正在编写小书签javascript.这里的问题是用户可以在某些页面完成加载之前和之后调用它.我想确保仅在页面加载完成后才运行sript.该怎么做?
I'm writing a bookmarklet javascript. The problem here is that it can be invoked by user before and after some page has finished loading. I want to ensure that the sript is run only after the page has finished loading. How to do that?
推荐答案
检查文档是否已加载的一种方法是检查 document.readyState 属性. IE,Firefox 3.6 +,Webkit和Opera支持它.
One way for checking if document has loaded, is to check the document.readyState property. IE, Firefox 3.6+, Webkit and Opera support it.
if (document.readyState === "complete") {
// do sth
}
另一件事是,如果要等待文档加载.在这种情况下,您必须听一些事件,例如文档上的DOMContentLoaded,窗口上的加载或文档上的readyStateChange.
Another thing is, if you want to wait for the document to load. In that case you have to listen to some events, like DOMContentLoaded on document, load on window or readyStateChange on document.
这篇关于检查(从bookmarklet)是否已加载页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!