Get the percentage of the page load using javascript?

馋奶兔 提交于 2019-12-23 07:05:53

问题


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


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/1958624/get-the-percentage-of-the-page-load-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!