本文介绍了检查jQuery是否存在Internet连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查使用jQuery的互联网连接?这样,我可以有一些条件说在生产期间使用Google缓存的JQuery版本,在开发过程中使用该版本或本地版本,具体取决于Internet连接".

How do you check if there is an internet connection using jQuery? 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".

推荐答案

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

在关闭</body>标记之前的位置:

Right before your close </body> tag:

<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>

鉴于您的问题集中在jQuery上,这可能是最简单的方法.

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

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

var online = navigator.onLine;

详细了解 W3C的脱机Web应用规范,但是请注意,这将在现代Web浏览器中最好地发挥作用,而对于较旧的Web浏览器可能无法按预期运行,或者根本无法运行.

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.

或者,向您自己的服务器发出XHR请求并不是测试连接性的一种方法.考虑到其他答案之一表明,XHR的故障点太多,如果您的XHR在建立连接时有缺陷,那么无论如何在常规使用中也有缺陷.如果您的站点由于某种原因无法访问,则在同一服务器上运行的其他服务也可能无法访问.该决定由您决定.

Alternatively, an XHR request to your own server isn't that bad of a method for testing your connectivity. 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. 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.

对于这种情况,我不建议向他人的服务(甚至是google.com)发出XHR请求.向服务器发出请求,或者根本不发出请求.

I wouldn't recommend making an XHR request to someone else's service, even google.com for that matter. Make the request to your server, or not at all.

在线"的含义似乎有些混乱.考虑到互联网是一堆网络,但是有时您使用的是VPN,却无法访问一般"互联网或万维网.公司通常拥有自己的网络,这些网络与其他外部网络的连接受到限制,因此可以将您视为在线".联机仅意味着您已连接到 a 网络,而不是您尝试连接的服务的可用性或可达性.

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. 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;
  }

}

您还可以在此处找到要点: https://gist.github.com/jpsilvashy/5725579

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

有关本地实施的详细信息

有些人评论说:我总是被错误地送回".那是因为您可能正在本地服务器上对其进行测试.无论您向哪个服务器发出请求,您都必须能够响应HEAD请求,当然您也可以将其更改为GET.

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.

这篇关于检查jQuery是否存在Internet连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 15:49