如何检查用户是否连接到互联网
在我的页面中,我必须从第三方站点读取天气信息,然后如果用户连接到互联网,则在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我知道的每个 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
)对于您的用例,最好的方法是检查您正在加载的天气内容的错误/返回代码。如果出现某种错误或无法加载,请显示您的本地内容。
如果您仍然想坚持通过加载 JS 库来检查连接性,您可以执行类似于以下操作:
可能将 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:
Possibly replacing 1.7.0.0 with your desired version of prototype.js
检查 jquery 是否已加载怎么样?
这可能会有所帮助.. http://jquery- howto.blogspot.com/2009/03/check-if-jqueryjs-is-loaded.html
当您将支票从
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
Does it help when you change your check from
if ($)
toif (jquery)
?