如何使用javascript或vbscript检查网址是否已完全加载?

时间:2018-02-19 09:27:02

标签: javascript vbscript

我有一组url.I需要检查这些url是否已完全加载并记录页面完全加载所需的时间。如果我知道怎么知道我可以知道时间如果使用java脚本或vbscript完全加载该URL

1 个答案:

答案 0 :(得分:0)

您可以使用 javascript 来控制iframe活动



var beforeLoad;
function newSite() {
   beforeLoad = (new Date()).getTime();
   $("#res").html('loading...');
   //loadURL(document.getElementById('urltext').value);//to deal with The X-Frame-Options response header
   document.getElementById('myframe').setAttribute('src', document.getElementById('urltext').value) ;
} 
$('#myframe').load(function(){
	$("#res").html('This request took '+(new Date().getTime() - beforeLoad)+' ms');

});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" value="https://en.wikipedia.org" id="urltext" />
<input type="button" value="Go" onClick="newSite()" />
<h3 id="res"> </h3>
<br>
<iframe id="myframe" src=""></iframe>
&#13;
&#13;
&#13;

它可以处理X-Frame-Options响应头使用:

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" value="" id="urltext" />
<input type="button" value="Change site" onClick="newSite()" />
<h3 id="res"> </h3>
<br>
<iframe id="myframe" src=""></iframe>


<script>
var beforeLoad;
function newSite() {
   beforeLoad = (new Date()).getTime();
   $("#res").html('loading...');
   loadURL(document.getElementById('urltext').value);
} 
$('#myframe').load(function(){
	$("#res").html('This request took '+(new Date().getTime() - beforeLoad)+' ms');

});

</script>
<script>
//deal with The X-Frame-Options response header
var iframe = document.getElementsByTagName('iframe')[0];
var url = iframe.src;
var getData = function (data) {
    if (data && data.query && data.query.results && data.query.results.resources && data.query.results.resources.content && data.query.results.resources.status == 200) loadHTML(data.query.results.resources.content);
    else if (data && data.error && data.error.description) loadHTML(data.error.description);
    else loadHTML('Error: Cannot load ' + url);
};
var loadURL = function (src) {
    url = src;
    var script = document.createElement('script');
    script.src = 'http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20data.headers%20where%20url%3D%22' + encodeURIComponent(url) + '%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=getData';
    document.body.appendChild(script);
};
var loadHTML = function (html) {
    iframe.src = 'about:blank';
    iframe.contentWindow.document.open();
    iframe.contentWindow.document.write(html.replace(/<head>/i, '<head><base href="' + url + '"><scr' + 'ipt>document.addEventListener("click", function(e) { if(e.target && e.target.nodeName == "A") { e.preventDefault(); parent.loadURL(e.target.href); } });</scr' + 'ipt>'));
    iframe.contentWindow.document.close();
}
//---
</script>
&#13;
&#13;
&#13;