chrome 判断网络环境_浏览器检测,移动网络的在线离线及网络状态

使用JS变量 navigator.onLine  是true还是false,可以判断在线还是离线。

不知道是不是html5的功能,在IE7,FF,Chrome下都是ok的。

如果你还想知道网络状况是wifi,还是2G,3G什么的,有少数部分浏览器支持network information api

浏览器支持情况 链接 http://mobilehtml5.org/ (这个内容很给力,html5的功能支持情况概览)

不过api文档说的接口属性好像不是那么回事。

我在android 2.3.6内置浏览器测试得出以下结论:

navigator.connection的属性有下面这几个

type 这个就表示当前网络状态了,值是下面几个常量中的一个。

UNKNOWN = 0

ETHERNET = 1

WIFI = 2

CELL_2G = 3

CELL_3G = 4

UC浏览器无此变量。

QQ浏览器有此变量,不过检测出来的type是0

至于online offline事件检测,可以通过监控下面的事件进行检测:

在android2.3.6内置浏览器测试通过,QQ浏览器测试不通过。

window.addEventListener('online' , function(){alert('online');},false);

window.addEventListener('offline' , function(){alert('offline');},false);

online,offline事件官方说明:

Returns false if the user agent is definitely offline (disconnected from the network). Returns true if the user agent might be online.

The events

The navigator.onLine attribute must return false if the user agent will not contact the network when the user follows links or when a script requests a remote page (or knows that such an attempt would fail), and must return true otherwise.

When the value that would be returned by the queue a task to fire a simple eventnamed offline at the

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值