0%

JS判断设备系统和微信打开

使用JS来判断当前设备的系统,主要用到的就是JS的Navigator对象的userAgent属性。下面列举一些常用设备的匹配规则:


var ua = navigator.userAgent.toLowerCase();
var IsIpad = ua.match(/ipad/i) == “ipad”;
var IsIphoneOs = ua.match(/iphone os/i) == “iphone os”;
var IsAndroid = ua.match(/android/i) == “android”;
var IsWM = ua.match(/windows mobile/i) == “windows mobile”;
var isWeixin = ua.match(/micromessenger/i) == “micromessenger”;
alert(‘isWeixin:’+isWeixin+’,IsIpad:’+IsIpad+’,IsIphoneOs:’+IsIphoneOs+’,IsAndroid:’+IsAndroid);

这里主要是有一个微信的判断,这个在做通过一个地址判断设备来进相应APP下载商店的功能需求时会用到,因为微信屏蔽了安卓的一些下载商店的地址,所以可能需要先判断微信打开来跳微信自己的下载商店,然后再判断设备跳转。
下面再给一个判断PC和移动设备的例子:


if(/AppleWebKit.*mobile/i.test(navigator.userAgent) || /MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent)){
if( window.location.href.indexOf(“?mobile”) < 0 ){
try{
if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){
window.location.href=”手机页面”;
}else if(/iPad/i.test(navigator.userAgent)){
window.location.href=”平板页面”;
}else{
window.location.href=”其他移动端页面”
}
}catch(e){}
}
}

参考:caibaojian.com/js-mobile-url.html