如何检查用户是否连接到互联网

发布于 2024-11-13 09:05:54 字数 981 浏览 0 评论 0原文

在我的页面中,我必须从第三方站点读取天气信息,然后如果用户连接到互联网,则在 div 中显示天气。如果没有,我会显示一些本地内容。所以我必须检查用户是否有连接。

有人说如果用户能看到我的页面,他们一定有连接。然而,情况并非总是如此,因为我们的应用程序可能在本地网络中运行,所以他们可以获取我们网站中的资源,但不能获取互联网中的资源。

我曾想过使用这种方式:在页面头部添加以下代码:

<head>
    <script src="http://www.google.com/xxx/jquery.min.xx.js" type="text/javascript"></script>
    <script type="text/javascript">
      window.onload=function(){
        if($){
          //the jquery is loaded,the client muse have the connection to the internet.
        } else
          //show local content
      }

    </script>
</head>

但是,在我们的页面中我们使用了原型,如果jquery加载成功,可能会导致冲突。

有什么想法吗?

笔记: 我们的页面中不需要jquery,这里我加载jquery只是为了测试连接,如果可能的话,我可以加载任何其他lib。

现在,我找到一个想法:

如果谷歌托管一个js文件internet.js,只需一行:

var xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx="xx";

然后我请求该文件,以测试是否定义了“xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”变量。 :)))

In my page,I have to read the weather information from a third part site,then show the weather in the div if the user has the connection to the internet. If not,I will show some local content instead. So I have to check if the user have the connection.

Some people said that if the user can see my page,they must be have the connection. However it is not true all the time,since our application may run in the local network,so they can get the resource in our site but not the resource in the internet.

I have thougth use this manner:add the following code in the head of the page:

<head>
    <script src="http://www.google.com/xxx/jquery.min.xx.js" type="text/javascript"></script>
    <script type="text/javascript">
      window.onload=function(){
        if($){
          //the jquery is loaded,the client muse have the connection to the internet.
        } else
          //show local content
      }

    </script>
</head>

However,in our page we have used the prototype ,if the jquery is loaded successfully,it may cause conflict.

Any idea?

NOTE:
we do not need the jquery in our page,here I load the jquery just for test the connection,if possible, I can load anyother lib .

Now,I find an idea:

if google host a js file internet.js,just one line:

var xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx="xx";

Then I requset this file ,to test if the "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" variale is defined. :)))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦初启 2024-11-20 09:05:54

我知道的每个 JS 库(Mootools/JQuery)在执行 AJAX 时,也会管理不同的返回码。这些代码之一是“服务器无法访问”(查找相关的数字代码)。您可以检查返回代码并对其进行操作。了解您使用的特定 API 以了解如何获取这些代码。

既然您说有时用户不会有外部连接,我会尝试包含(JSONP)来自查询站点的脚本,该脚本依次加载实际内容,而该脚本未激活,则显示本地内容。我将使用定时循环(setInterval)来完成整个过程

Each JS library I know (Mootools/ JQuery) when they do AJAX, they also manage different return codes. One of those codes is "server not reachable" (find the relevant numeric code). You can check the return code and act upon it. Learn the specific API you use to see how to get those codes.

Since you say that some times the user won't have an outside connection, I would try to include (JSONP) a script from the queried site, which in turn loads the actual content, while this script is not active, show the local content. I would do this entire process using timed loop (setInterval)

油焖大侠 2024-11-20 09:05:54

对于您的用例,最好的方法是检查您正在加载的天气内容的错误/返回代码。如果出现某种错误或无法加载,请显示您的本地内容。

如果您仍然想坚持通过加载 JS 库来检查连接性,您可以执行类似于以下操作:

<script src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"></script>
<script>
    if (typeof window.$ !== 'function') {
         window.noInternet = true;
         document.write('<script src="http://mylocalserver/prototype.js"></scri' + 'pt>');
    }
</script>

可能将 1.7.0.0 替换为您想要的 prototype.js 版本

For your usecase, the best way would be to check the error/return code for the weather content you're loading. If it somehow errors or doesn't load, display your local content.

If you still want to insist on checking connectivity by loading a JS library, you can do something akin to the following:

<script src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"></script>
<script>
    if (typeof window.$ !== 'function') {
         window.noInternet = true;
         document.write('<script src="http://mylocalserver/prototype.js"></scri' + 'pt>');
    }
</script>

Possibly replacing 1.7.0.0 with your desired version of prototype.js

情魔剑神 2024-11-20 09:05:54

检查 jquery 是否已加载怎么样?
这可能会有所帮助.. http://jquery- howto.blogspot.com/2009/03/check-if-jqueryjs-is-loaded.html

注意:这里我们检查 jQuery
函数是否被定义。这是
检查 jQuery 库的安全方法
正在加载。如果你不是
使用任何其他 JavaScript 库
像prototype.js或mootools.js,然后
您还可以检查 $ 而不是
jQuery。

当您将支票从 if ($) 更改为 if (jquery) 时,它是否有帮助?

What about checking if jquery is loaded?
This might help.. http://jquery-howto.blogspot.com/2009/03/check-if-jqueryjs-is-loaded.html

NOTE: Here we are checking for jQuery
function being defined or not. This is
a safe way to check for jQuery library
being loaded. In case you are not
using any other javascript libraries
like prototype.js or mootools.js, then
you can also check for $ instead of
jQuery.

Does it help when you change your check from if ($) to if (jquery)?

灯角 2024-11-20 09:05:54
if(navigator.onLine){
 //enter code here
}
if(navigator.onLine){
 //enter code here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文