admin管理员组文章数量:1415139
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?
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?
Share Improve this question asked Nov 24, 2010 at 18:54 mmierinsmmierins 3,7944 gold badges23 silver badges25 bronze badges 3- So far, no-one has understood the question. He wants to find out whether the document has finished loading yet. – SLaks Commented Nov 24, 2010 at 18:59
- 1 lol, u jerk, thanks for the down vode... document ready does figure out when the document has finished loading... what else would you think it does... make tea? – CrazyDart Commented Nov 24, 2010 at 19:16
- @CrazyDart while I don't agree with your language I do agree with the sentiment. The question wasn't asked very clearly if 80% of the responders answered the same way. Maybe the question needs a down vote? This is answered by all the respondents you downvoted => I want to ensure that the sript is run only after the page has finished loading. The event onload is called AFTER the page is finished loading. I think that is a valid answer. Again, thanks for the downvotes. – SRM Commented Nov 24, 2010 at 19:18
5 Answers
Reset to default 3One 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 === "plete") {
// do sth
}
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.
Hooking a function to the document's ready/load function ensures nowhere in your code can be executed before the DOM is loaded.
<html>
<body onload="documentLoad()">
<script type="text/javascript">
function documentLoad() {
alert("Document is ready now.");
}
</script>
</body>
</html>
If you want to use jQuery, you can use a very short-hand method to attach all your code to the ready function.
$(document).ready(function() {
// All code in here - will trigger when DOM is loaded.
}
And here is an even shorter short-hand method, using jQuery, to achieve the same.
$(function(){
// All code in here - will trigger when DOM is loaded.
});
<body onload="start()">
</body>
In the function start the DOM is guaranteed to be loaded.
var firstLoad = true;
$(document).ready(function(){
if ( firstLoad ){
firstLoad=false;
//your code to run once
}
});
Most answers here are answering how to attach a function to the document ready function, which isn't what davidgale is asking...
The document
object has a property of readyState
which will be set to "plete"
when it has finished loading.
<html>
<body>
<script type="text/javascript">
function someFunction() {
// Called various times throughout page's life.
if(document.readyState == "plete") {
// Perform DOM actions - will only attempt after DOM is loaded.
}
}
</script>
</body>
</html>
本文标签: javascriptCheck (from bookmarklet) whether page is loadedStack Overflow
版权声明:本文标题:javascript - Check (from bookmarklet) whether page is loaded? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745227823a2648689.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论