Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
452 views
in Technique[技术] by (71.8m points)

javascript - 检查Internet连接是否存在Javascript? [重复](Check if Internet Connection Exists with Javascript? [duplicate])

How do you check if there is an internet connection using Javascript?

(您如何使用Javascript检查互联网连接?)

That way I could have some conditionals saying "use the google cached version of JQuery during production, use either that or a local version during development, depending on the internet connection".

(这样,我可以有一些条件说:“在生产过程中使用jQuery的google缓存版本,在开发过程中使用该缓存或本地版本,具体取决于Internet连接”。)

  ask by Lance Pollard translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The best option for your specific case might be:

(针对您的特定情况的最佳选择可能是:)

Right before your close </body> tag:

(在您的</body>标记之前:)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.10.2.min.js"></script>')</script>

This is probably the easiest way given that your issue is centered around jQuery.

(考虑到您的问题围绕jQuery,这可能是最简单的方法。)

If you wanted a more robust solution you could try:

(如果您想要一个更强大的解决方案,可以尝试:)

var online = navigator.onLine;

Read more about the W3C's spec on offline web apps , however be aware that this will work best in modern web browsers, doing so with older web browsers may not work as expected, or at all.

(阅读有关W3C脱机Web应用程序规范的更多信息,但是请注意,这将在现代Web浏览器中最有效,使用较旧的Web浏览器可能无法按预期运行,或者根本无法运行。)

Alternatively, an XHR request to your own server isn't that bad of a method for testing your connectivity.

(另外,向您自己的服务器发出XHR请求并不是测试连接性的一种方法。)

Considering one of the other answers state that there are too many points of failure for an XHR, if your XHR is flawed when establishing it's connection then it'll also be flawed during routine use anyhow.

(考虑到其他答案之一表明,XHR的故障点太多,如果您的XHR在建立连接时有缺陷,那么无论如何在日常使用中也有缺陷。)

If your site is unreachable for any reason, then your other services running on the same servers will likely be unreachable also.

(如果您的站点由于某种原因无法访问,则在同一服务器上运行的其他服务也可能无法访问。)

That decision is up to you.

(该决定由您决定。)

I wouldn't recommend making an XHR request to someone else's service, even google.com for that matter.

(我不建议对此人甚至向google.com提出XHR请求。)

Make the request to your server, or not at all.

(向服务器发出请求,或者根本不发出请求。)

What does it mean to be "online"?(“在线”意味着什么?)

There seems to be some confusion around what being "online" means.

(关于“在线”的含义似乎有些混乱。)

Consider that the internet is a bunch of networks, however sometimes you're on a VPN, without access to the internet "at-large" or the world wide web.

(考虑到互联网是一堆网络,但是有时您使用的是VPN,无法访问“一般”互联网或万维网。)

Often companies have their own networks which have limited connectivity to other external networks, therefore you could be considered "online".

(公司通常都有自己的网络,这些网络与其他外部网络的连接受到限制,因此可以将您视为“在线”。)

Being online only entails that you are connected to a network, not the availability nor reachability of the services you are trying to connect to.

(正在网上只需要您连接到网络 ,你试图连接到服务的不可用性也可达性。)

To determine if a host is reachable from your network, you could do this:

(要确定主机是否可以从您的网络访问,可以执行以下操作:)

function hostReachable() {

  // Handle IE and more capable browsers
  var xhr = new ( window.ActiveXObject || XMLHttpRequest )( "Microsoft.XMLHTTP" );

  // Open new request as a HEAD to the root hostname with a random param to bust the cache
  xhr.open( "HEAD", "//" + window.location.hostname + "/?rand=" + Math.floor((1 + Math.random()) * 0x10000), false );

  // Issue request and handle response
  try {
    xhr.send();
    return ( xhr.status >= 200 && (xhr.status < 300 || xhr.status === 304) );
  } catch (error) {
    return false;
  }

}

You can also find the Gist for that here: https://gist.github.com/jpsilvashy/5725579

(您也可以在这里找到要点: https : //gist.github.com/jpsilvashy/5725579)

Details on local implementation

(当地实施细节)

Some people have commented, "I'm always being returned false".

(有人评论说:“我总是被错误地送回”。)

That's because you're probably testing it out on your local server.

(那是因为您可能正在本地服务器上对其进行测试。)

Whatever server you're making the request to, you'll need to be able to respond to the HEAD request, that of course can be changed to a GET if you want.

(无论您向哪个服务器发出请求,您都必须能够响应HEAD请求,如果需要,当然可以将其更改为GET。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...