Get the percentage of the page load using javascri

2019-06-15 14:10发布

I'm writing an iphone application and need to show a progress bar that shows the loading progress of a web page. I want to insert a JS function to this page and once I call it, it will give me the load progress (how much bytes have been loaded and the total size). Is this possible?

Thanks

4条回答
Fickle 薄情
2楼-- · 2019-06-15 14:48

How do you know what percentage of the HTML is loaded by Safari? Javascript from what I know of 1.7 can never be aware of that. If it did you would have far more sophisticated loaders for Gmail and the wealth of Google apps. You can do very crude estimations by injecting scripts and elements into the DOM programatically but it's a lot of effort for not much gain.

The best you can hope for is an animated GIF that runs for how long you'd estimate the page to load in a worst case scenario. Or the easier solution is to just use a throbber, hour glass or barber's pole.

查看更多
Deceive 欺骗
3楼-- · 2019-06-15 14:50

No, it's not possible. You can emulate it by breaking up your page to small pieces and load it one by one with ajax requests but I don't think it is worth the trouble.

Another idea is to put little pieces of script like

<script>percentage += 10; updateProgressBar();</script>

through your page. That script will be executed the second browser loads (or parses) it so you will be able to estimate the progress.

查看更多
趁早两清
4楼-- · 2019-06-15 14:53
<html>
<head>
</head>
<body>
<script type="text/javascript" type="javascript">
var percent=0;

function curpercent(){
    //TODO : show percent : $("#lblpercent").html(percent + "%");
    if (percent <=100) {
        curpercent();
    }

}

curpercent(); //call curpercent function when page download this line, coz, browser always download your page line by line.

</script>

text body 1
<script type="text/javascript" type="javascript">
percent=10;
</script>

text body 2
<script type="text/javascript" type="javascript">
percent=20;
</script>

...

text body 10
<script type="text/javascript" type="javascript">
percent=100;
</script>

</body>
</html>
查看更多
Bombasti
5楼-- · 2019-06-15 15:12

I don't think it is possible using Javascript, and unless the page is VERY big, I don't see why you'd need this. If you have lots of images on the page, this may be possible to tell how many of them are fully loaded.

Edit: I found this, that looks like want you want to do.

Edit 2 : And this answer on SO.

查看更多
登录 后发表回答