javascript 检查网络服务器是否在线/离线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23997675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:57:39  来源:igfitidea点击:

Checking if web server is online/offline

javascriptjqueryjqxhr

提问by Nathan

How would I check if a web server is online/offline from my domain? I've already tried an $ajax call with jsonp, but if the server isn't responding with json, then that doesn't work. Is there any other way to get an http response from a server?

我如何检查网络服务器是否从我的域在线/离线?我已经尝试过使用 jsonp 进行 $ajax 调用,但是如果服务器没有使用 json 进行响应,那么这将不起作用。有没有其他方法可以从服务器获取 http 响应?

回答by Hyman_of_All_Trades

Just copied shamelessly from one website for easiness:

为了方便起见,只是从一个网站上无耻地复制了:

<img src="http://site-to-check.com/online.png" alt="status" onerror="this.src='files/offline.png'" />

The setup is:

设置是:

An online icon (online.png) on the remote server you want to see the status of. An offline icon (offline.png) on the server that is going to show the status page.

要查看其状态的远程服务器上的在线图标 (online.png)。服务器上的脱机图标 (offline.png) 将显示状态页面。

If the image on the remote server fails to load, the onerror event is triggered and the javascript rewrites the image tag to show the offline image. You can use relative or absolute paths for the offline image. This snippet is fully compatible with all major browsers, including Internet Explorer down to version 5.5!

如果远程服务器上的图片加载失败,则触发 onerror 事件,javascript 重写图片标签以显示离线图片。您可以为离线图像使用相对或绝对路径。此代码段与所有主要浏览器完全兼容,包括 Internet Explorer 低至 5.5 版!

回答by V319

As i understand, you want to know, if the website is online\offline How about this solution?

据我了解,您想知道,如果网站在线\离线 ,这个解决方案如何?