如何使用JavaScript检查网站是否在线?加上有关如何使此功能的其余部分更好地工作的提示 [英] How to check if site is online with JavaScript? Plus tips on how to make the rest of this function work better
问题描述
我需要检查网站是否在线并获取ping时间.
I need to check if a website is online and to get ping time.
我已经有一个大概的ping时间(足够接近).但是我仍然需要检查网站是否在线,并使该功能运行五次,并获得最低的ping值(以使其更准确).
I already have a rough ping time (close enough). But I still need to check if the site is online and make the function run five times, and get the lowest ping value (in order to get it more accurate).
到目前为止,我的代码:
My code so far:
<script>
function initPingTest(siteUrl){
pingTester();
function pingTester(){
document.getElementById('site').innerHTML = '<center>' + siteUrl + '</center>';
document.getElementById('pingT').innerHTML = '<center> testing.. </center>';
var startTime=new Date();
window.startTime=startTime.getTime();
var imgSrc=siteUrl+ '?cacheBuster='+Math.random();
var iFrame=document.getElementById('myPingTest');
iFrame.onload=pingTime;
iFrame.src=imgSrc;
function pingTime() {
var endTime=new Date();
var pingResult = (endTime.getTime()-window.startTime);
if(pingResult<hidden.hidden.value){
document.getElementById('pingT').innerHTML = '<center>' + pingResult + '</center>';
hidden.hidden.value=pingResult;
}else{document.getElementById('pingT').innerHTML = '<center>' + hidden.hidden.value + '</center>';}
}
}
}
</script>
<IFRAME style="display:none;" id=myPingTest></IFRAME>
<FORM NAME="hidden" STYLE="display:none"><INPUT TYPE="text" NAME="hidden" value="100000"></form>
<br>
<FORM method="get" NAME="ping" action="javascript:initPingTest(ping.url.value);"><font>Enter site adress(ie.www.yahoo.com)<br>http://</font>
<INPUT TYPE="text" NAME="url" size="18"><input type="submit" value="Ping"/></form>
<br>
<table cellspacing=5px>
<tr><th>
Url:
</th><th>
Ping Time (miliseconds)
</th></tr>
<tr><td>
<label id=site><center> - </center></label>
</td><td>
<label id=pingT><center> - </center></lable>
</td></tr>
</table>
</body></html>
推荐答案
虽然我不相信Javascript确实提供了这种功能,但是您可以创建一个xmlhttprequest对象,并比较发送时的区别.以及连接的时间.
While I don't believe Javascript 'really' offers this kind of functionality, you could create an xmlhttprequest object, and perhaps compare the difference from the time it sends with the time it connects.
以下代码显示了实现此目的的一种可能方法:如果服务器现在关闭,应该发出警报
The following code shows a possible way to achieve this: should alert if the server is down now
var address = "http://www.google.com";
var t1 = Date.now();
var t2;
var max = 100000;
var failed = false;
var httpReq = (window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
if(httpReq == null) {
console.log("Error: XMLHttpRequest failed to initiate.");
}
httpReq.onreadystatechange = function() {
var failTimer = setTimeout(function() {
failed = true;
httpReq.abort();
}, max);
if (httpReq.readyState == 4) { //Completed loading
if (!failed && (httpReq.status == 200 || httpReq.status == 0)) {
clearTimeout(failTimer);
t2 = Date.now();
var timeTotal = (t2 - t1);
if(timeTotal > max) {
onFail();
} else {
onSuccess(timeTotal);
}
}
else { //Otherwise, there was a problem while loading
console.log("Error " + httpReq.status + " has occurred.");
onFail();
}
}
}
try {
httpReq.open("GET", address, true);
httpReq.send(null);
} catch(e) {
console.log("Error retrieving data httpReq. Some browsers only accept cross-domain request with HTTP.");
onFail();
}
function onSuccess(timeTotal) {
alert("Time needed to connect: " + timeTotal);
}
function onFail() {
alert("Well this isn't good");
}
iframe也可能是可行的解决方案.
An iframe may be a viable solution as well though.
这篇关于如何使用JavaScript检查网站是否在线?加上有关如何使此功能的其余部分更好地工作的提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!