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
Add a ment  | 

5 Answers 5

Reset to default 3

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 === "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