开发者

Is there a definitive JavaScript (not jQuery) method for checking whether or not a web page has loaded completely?

Is there a definitive JavaScript method for checking whether 开发者_运维知识库or not a web page has loaded completely? Completely, meaning 100% complete. HTML, scripts, CSS, images, plugins, AJAX, everything!

As user interaction can effect AJAX, let's assume there is no further user interaction with the page, apart from the initial page request.


What you're asking for is pretty much impossible. There is no way to determine whether everything has loaded completely. Here's why:

  • On a lot of webpages, AJAX only starts once the onload (or DOMReady) event fires, making the method of using the onload event to see if the page has loaded impossible.
  • You could theoretically tell if the webpage was performing an AJAX request by overriding window.XMLHttpRequest, but you still couldn't tell if plugins like Flash were still loading or not.
  • On some sites, like Twitter.com, the page only loads once and simply makes AJAX requests to the server every time the user clicks a link. How do you tell if the page has finished loading on a page like that?
  • In fact, the browser itself can't be completely certain whether the page has completely finished loading, or whether it's about to make more AJAX requests.

The only way to know for sure that everything loaded is to have every single piece of code on the page that loads something tell your code that it has finished once it loads.


A hacky, incomplete solution: You could try overriding XMLHttpRequest with a function that wraps the existing XMLHttpRequest and returns it. That would allow you to tell if a AJAX event is currently taking place. However, that solution wouldn't work for seeing if plugins are loaded, and you wouldn't be able to tell the difference between AJAX events that are triggered at page load and AJAX requests that happen periodically, like the ones on Stack Overflow that change the Stack Exchange icon on the top-left if you have new notifications.

Try something like this:

(function(oldHttpRequest){
  // This isn't cross-browser, just a demonstration
  // of replacing XMLHttpRequest

  // Keep track of requests
  var requests_running = 0;

  // Override XMLHttpRequest's constructor
  window.XMLHttpRequest = function() {
    // Create an XMLHttpRequest
    var request = new oldHttpRequest();

    // Override the send method
    var old_send = request.send;
    request.send = function () {
      requests_running += 1;
      old_send.apply(request, arguments);
    };

    // Wait for it to load
    req.addEventListener("load", function() {
      requests_running -= 1;
    }, false);

    // Return our modified XMLHttpRequest
    return request;
  };

  window.addEventListener("load", function() {
    // Check every 50 ms to see if no requests are running
    setTimeout(function checkLoad() {
      if(requests_running === 0)
      {
        // Load is probably complete
      }
      else
        setTimeout(checkLoad, 50);
    }, 50);
  }, false);
})(window.XMLHttpRequest)


The:

window.onload

event will fire at this point.


window.onLoad = function(){
    //Stuff to do when page has loaded.
}

or

<body onLoad="functionCall()">


Basically ADW and Keith answer the question, but I would suggest not to use window.onload but:

if (window.addEventListener) {
    window.addEventListener("load", myfunction, false);
} else {
    window.attachEvent("onload", myfunction);
}

function myfunction() {
   ...
}


Using a combination of window.onload, document.readyState, and callbacks for AJAX requests, you should be able to do what you want. Simply make sure the window has loaded, the DOM is ready for manipulation, and keep track of AJAX requests.

For AJAX in particular, depending on how many requests you make: Increment a variable each time you make a request, and when the variable === the total amount of requests, fire a function. If you don't happen to know the amount of AJAX requests, but know which one would be last, simply have a callback function fire when it finishes.

When all is set and true, fire a final function to do what you want, knowing everything should be loaded.

In regards to Flash and Silverlight applications (not sure if window.onload or document.ready keeps track of those), you could also record the amount of data loaded withing the application, and when the loaded data === the total data, have the application fire a function or increment a variable to the page.

window.onload = function() {
    var time = window.setInterval(function() {
     if(document.readyState == "interactive") { 
         increment(); 
         window.clearInterval(time);
     }
    }, 250);
}

var total = 10, current = 0;
var increment = function() {
    current += 1;
    if(current === total) { weAreDone(); }    
}

function weAreDone() {
    // Everything should be done!
}


Here is the non intrusive js function I scripted, using events on load. In this case, I fire events on js script load as this is my js autoloader function, but you can just add event on other items using the same principle. Provided this script looks after js scripts loaded in a dedicated div tag.

function scriptLoaded(e) {
    var oLoadedScript = e.target || e.srcElement;
    alert ('loaded : ' + oLoadedScript.src);
    return false;
}

/**
 * Import js lib and fire function ControlData on events
 * @param js_librairies
 * @returns {Boolean}
 */
function init(){

    // lib import
    // Locate js in the div
    var myscript_location = document.getElementById('js_script_goes_here');


    // DEBUG
    if (undefined == myscript_location)
            alert('div not found');
        else
            alert('found div : ' + myscript_location);

 // to prevent js script from catching in dev mode
    var force_js_reload = "?version=1" ;

    for (var i=0; i < js_librairies.length ; ++i) {
        var my_script = document.createElement('script');
        my_script.defer = false;
        my_script.src = relative_path + js_librairies[i] + force_js_reload ;
        my_script.type = 'text/javascript';
        // DEBUG
        my_script.onload = scriptLoaded;

        myscript_location.appendChild(my_script);

    }
    return false;
}

/**
 * Start non intrusive js
 * @param func
 */
function addLoadEvent(func) {
      var oldonload = window.onload;
      if (typeof window.onload != 'function') {
        window.onload = func;
      } else {
        window.onload = function() {
          if (oldonload) {
            oldonload();
          }
          func();
        };
      }
}


//ONLOAD
addLoadEvent(init);


function r(f){/in/(document.readyState)?setTimeout(r,9,f):f()}

Courtesy: Smallest DOMReady code, ever - Dustin Diaz

Update: And for IE

function r(f){/in/.test(document.readyState)?setTimeout('r('+f+')',9):f()}

P.S: window.onload is a very different thing

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜