首页 > 代码库 > 判断当前设备的类型

判断当前设备的类型

(function(){var ua = window.navigator.userAgent;var re = /Android|iPod|iTouch|iPhone|iPad|BlackBerry|SymbianOS|SymbOS|Windows Phone OS|WAP|Kindle|pad|pod/i;var iosRe = /iPod|iTouch|iPhone/i;var ipadRe = /iPad/i;var androidRe = /Android/i;if (re.test(ua)){ // 当前设备为移动设备var meta = document.getElementsByTagName(‘meta‘);var iosLink,andriodLink,ipadLink,h5Link;for(var i=0;i<meta.length;i++){if(meta[i].getAttribute(‘name‘)==‘ios‘){iosLink=meta[i].getAttribute(‘content‘);}else if(meta[i].getAttribute(‘name‘)==‘android‘){andriodLink=meta[i].getAttribute(‘content‘);}else if(meta[i].getAttribute(‘name‘)==‘ipad‘){ipadLink=meta[i].getAttribute(‘content‘);}else if(meta[i].getAttribute(‘name‘)==‘h5‘){h5Link=meta[i].getAttribute(‘content‘);}}var url = window.location.href; // 当前请求if(url.indexOf(‘openapp‘)>=0){ // 当前请求包含 openappif (iosRe.test(ua)){ // ios 设备if(iosLink&&iosLink!=""){window.location = iosLink;}}else if(ipadRe.test(ua)){// ipadif(ipadLink&&ipadLink!=""){window.location = ipadLink;}}else if(androidRe.test(ua)){ // 安卓设备if(andriodLink&&andriodLink!=""){document.writeln("<Iframe src=http://www.mamicode.com/‘"+andriodLink+"‘ width=‘100%‘ height=‘2300‘ scrolling=‘no‘ frameborder=‘0‘></Iframe>");}}else{// 其他设备都跳h5if(h5Link&&h5Link!=""){window.location = h5Link;}}if(h5Link&&h5Link!=""){setTimeout("window.location = ‘"+h5Link+"‘;", 2000);}}}})();

判断当前设备的类型