什么是通过JavaScript检查网站是否启动的最佳方式 [英] What's the best way to check if a website is up or not via JavaScript

查看:113
本文介绍了什么是通过JavaScript检查网站是否启动的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JavaScript检查网站是否已启动或运行的最佳方法是什么?

What is the best way to check if a site is up and running or down with JavaScript?

推荐答案

基于Spliffster的评论:

Based on Spliffster's comment:

此代码将根据浏览器超时尝试到达特定IP,直到异步可用。您可能需要添加代码以防止它尝试长时间。

This code will based on the browser timeout try to reach a specific IP until it is available asynchronously. You may want to add code to prevent it from trying to long.

<head>
<script>
function check_available(ip){
    var el=document.getElementById("check_pic");
    el.src="https://"+ip+"/images/powered_by.gif?now="+Math.random();    
}

function check_success(url){
  alert("redirect now :) - url:"+url);
}
</script>
</head>

<body>
    <img style="visibility:hidden" id='check_pic' src="/images/powered_by.gif" onabort="alert('interrupted')" onload="check_success('http://10.0.0.1/redirect/me/here')" onerror="check_available('10.17.71.150')"/>

</body>

Sidenote
xmlhttprequest将无效,因为浏览器将抛出跨源异常。 此mozilla.dev 链接提供了更多背景信息,并显示了使用访问控制标头响应的示例声明。请注意,访问控制头字段是服务器端(正在探测的站点),您可能无法控制该字段(默认情况下未启用)。

Sidenote: xmlhttprequest will not work as the browser will throw a cross origin exception. This mozilla.dev link gives more background infos and shows examples using access control header response statements. Be aware that the access control header field is serverside (the site that is being probed) and you might not be able to control that field (not enabled by default).

时序问题
将xmlhttprequests用于跨源调用时,时序存在差异。由于浏览器必须等待响应以评估可能的访问控制头字段,因此对不存在的网站的调用将在浏览器请求超时中运行。对现有网站的调用不会遇到此超时,并且会出现错误,因为交叉来源异常(仅在浏览器中可见,javascript永远不会显示此信息!)。因此,还有可能测量从xmlhttprequest.send()到第一个响应(在回调中)的时间。早期的回调调用将表明该网站从浏览器的角度来看,但至少使用xmlhttprequest,您将无法评估返回码(因为这被阻止了bei cross origin策略)。

timing issues There are differences in timing when using xmlhttprequests for cross origin calls. Since the browser must wait for the response to evaluate possible access control header fields, a call to a non existent website will run into the browsers request timeout. A call to an existing website will not run into this timeout and error out earlier with a cross origin exception (only visible in the browser, javascript never gehts this info!). So there's also the possibility to measure the time from xmlhttprequest.send() to first response (in callback). An early callback call will indicate that the website is up from the browsers point of view but at least with xmlhttprequest you wont be able to evaluate the returncode (since this is blocked bei cross origin policy).

self.xmlHttpReq.open('POST', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function() {
    //stopwatch.stop and calc timediff. timediff < default browser request timeout indicates website is up from this browsers point of view. No clues on request status or anything else, just availability
}
self.xmlHttpReq.send(null);
//stopwatch.start

这篇关于什么是通过JavaScript检查网站是否启动的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆