if(navigator.onLine){
...}else{ ...}
非常简单,但是并不准确-根据MDN的描述:
navigator.onLine只会在机器未连接到局域网或路由器时返回false,其他情况下均返回true。
也就是说,机器连接上路由器后,即使这个路由器没联通网络,navigator.onLine仍然返回true。
<script type="text/javascript">function browserRedirect() {
var sUserAgent = navigator.userAgent.toLowerCase()
var bIsIpad = sUserAgent.match(/ipad/i) == "ipad"
var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os"
var bIsMidp = sUserAgent.match(/midp/i) == "midp"
var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4"
var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb"
var bIsAndroid = sUserAgent.match(/android/i) == "android"
var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce"
var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile"
if (!(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) ){
window.location.href=B页面
}
}
browserRedirect() </script>
navigator.onLine
online和offline。当网络从离线变为在线或者从在线变为离线时,分别触发这两个事件。这两个事件在window对象上触发。(必须要手工轮询这个属性才能检测到网络状态的变化。 )
Offline.js 插件( https://github.com/hubspot/offline )