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?
-
So far, no-one has understood the question. **He wants to find out whether the document has finished loading yet.** – SLaks Nov 24 '10 at 18:59
-
1lol, 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 Nov 24 '10 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 Nov 24 '10 at 19:18
5 Answers
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
}
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.

- 18,349
- 5
- 58
- 67
-
BTW. some people also check for (document.readyState === "complete" || document.readyState === "loaded"). Don't know which browser uses the "loaded" value, but it must be a compatibility issue with older browsers. – Rafael Nov 24 '10 at 19:25
-
1'loaded' happens before 'complete' before the element is actually added to the DOM. It messed me up when I was dynamically adding `script` elements, and couldn't access their contents. – zzzzBov Nov 24 '10 at 19:41
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.
});

- 11,471
- 1
- 31
- 57

- 21,235
- 17
- 84
- 107
<body onload="start()">
</body>
In the function start the DOM is guaranteed to be loaded.

- 1,377
- 1
- 10
- 17
var firstLoad = true;
$(document).ready(function(){
if ( firstLoad ){
firstLoad=false;
//your code to run once
}
});

- 5,278
- 6
- 41
- 66
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 "complete"
when it has finished loading.
<html>
<body>
<script type="text/javascript">
function someFunction() {
// Called various times throughout page's life.
if(document.readyState == "complete") {
// Perform DOM actions - will only attempt after DOM is loaded.
}
}
</script>
</body>
</html>

- 21,235
- 17
- 84
- 107
-
From the OP: I want to ensure that the sript is run only after the page has finished loading. It sounds like that is exactly what he is asking. – SRM Nov 24 '10 at 19:37