admin管理员组

文章数量:1323732

Is there any way to know how far browser loaded the page?

Either by using JavaScript or browser native functions.

Based on the page status i want to build progress bar.

Is there any way to know how far browser loaded the page?

Either by using JavaScript or browser native functions.

Based on the page status i want to build progress bar.

Share Improve this question edited Feb 20, 2013 at 12:59 Ramesh Paul asked Feb 20, 2013 at 12:50 Ramesh PaulRamesh Paul 8604 gold badges15 silver badges33 bronze badges 4
  • why ? already you have browser tools. press F12. go for Network tab – Ravi Gadag Commented Feb 20, 2013 at 12:51
  • check in firebug -> Net tab – Ashwin Commented Feb 20, 2013 at 12:52
  • I think he wants to put some progress bar in his page to show the page loading status? – Gaurav Pandey Commented Feb 20, 2013 at 12:54
  • Yes i want to put progress bar based on page loading status – Ramesh Paul Commented Feb 20, 2013 at 12:56
Add a ment  | 

3 Answers 3

Reset to default 3

I'm not 100% sure this will work, but.. here is the theory:

First of all, don't stop JavaScript running until the page has loaded. Meaning, don't use window.ready document.ready etc..

At the top of the page initialise a JavaScript variable called loaded or something and set it to 0.

var loaded = 0;

Throughout the page increment loaded at different points that you consider to be at the correct percentages.

For example, after you think half the page would have been loaded in the code set loaded = 50;.

Etc..

As I say, this is just a concept.

Code:

function request() {
    showLoading(); //calls a function which displays the loading message

    myRequest = GetXmlHttpObject();
    url = 'path/to/script.php';
    myRequest.open('GET',url,true);
    myRequest.onreadystatechange = function() {
        if(myRequest.readyState == 4 && myRequest.status == 200) {
            clearLoading(); //calls a function which removes the loading message

            //show the response by, e.g. populating a div
            //with the response received from the server
        }
    }
    myRequest.send(null);
}

At the beginning of the request I call showLoading() which uses Javascript to dynamically add the equivalent of your preLoaderDiv. Then, when the response is received, I call clearLoading() which dynamically removes the equivalent of your preLoaderDiv.

You'll have to determine it yourself, and to do that you'll have to have a way to determine it. One possibility is to have dummy elements along the page, know their total, and at each point count how many are already present. But that will only give you the amount of DOM obtained, and that can be a very small part of the load time - most often than not, the browser is idle waiting for scripts and images.

本文标签: javascriptPage loading statusStack Overflow