通过JS语句判断WEB网站的访问端是电脑还是手机
1 <script type="text/javascript"> 2 <!-- 3 //平台、设备和操作系统 4 var system = { 5 win: false, 6 mac: false, 7 xll: false, 8 ipad:false 9 }; 10 //检测平台 11 var p = navigator.platform; 12 system.win = p.indexOf("Win") == 0; 13 system.mac = p.indexOf("Mac") == 0; 14 system.x11 = (p == "X11") || (p.indexOf("Linux") == 0); 15 system.ipad = (navigator.userAgent.match(/iPad/i) != null)?true:false; 16 //跳转语句,如果是手机访问就自动跳转到wap.baidu.com页面 17 if (system.win || system.mac || system.xll||system.ipad) { 18 19 } else { 20 21 window.location.href = "http://www.jdpatro.com/3g/"; 22 } 23 --> 24 </script>
腾讯网的适配代码
1 <script type="text/javascript"> 2 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))){ 3 if(window.location.href.indexOf("?mobile")<0){ 4 try{ 5 if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){ 6 window.location.href="http://shipei.qq.com/index.htm"; 7 }else if(/iPad/i.test(navigator.userAgent)){ 8 }else{ 9 window.location.href="http://shipei.qq.com/simple/s/index/" 10 } 11 }catch(e){} 12 } 13 } 14 </script>
1 <script type="text/javascript"> 2 var browser = { 3 versions : function() { 4 var u = navigator.userAgent, app = navigator.appVersion; 5 return {//移动终端浏览器版本信息 6 trident : u.indexOf('Trident') > -1, //IE内核 7 presto : u.indexOf('Presto') > -1, //opera内核 8 webKit : u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核 9 gecko : u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核 10 mobile : !!u.match(/AppleWebKit.*Mobile.*/) 11 || !!u.match(/AppleWebKit/), //是否为移动终端 12 ios : !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端 13 android : u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器 14 iPhone : u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器 15 iPad: u.indexOf('iPad') > -1, //是否iPad 16 webApp : u.indexOf('Safari') == -1,//是否web应该程序,没有头部与底部 17 google:u.indexOf('Chrome')>-1 18 }; 19 }(), 20 language : (navigator.browserLanguage || navigator.language).toLowerCase() 21 } 22 document.writeln("语言版本: "+browser.language); 23 document.writeln(" 是否为移动终端: "+browser.versions.mobile); 24 </script>
JS 判断浏览器客户端类型(ipad,iphone,android)
1 <script type="text/javascript"> 2 var bForcepc 3 = fGetQuery("dv") == "pc"; 4 function 5 fBrowserRedirect(){ 6 var sUserAgent = navigator.userAgent.toLowerCase(); 7 var bIsIpad = sUserAgent.match(/ipad/i) == 8 "ipad"; 9 var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os"; 10 var bIsMidp = sUserAgent.match(/midp/i) == "midp"; 11 var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4"; 12 var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb"; 13 var bIsAndroid = sUserAgent.match(/android/i) == "android"; 14 var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce"; 15 var bIsWM = sUserAgent.match(/windows mobile/i) == "windows 16 mobile"; 17 if(bIsIpad){ 18 var sUrl = 19 location.href; 20 if(!bForcepc){ 21 window.location.href = "http://ipad.mail.163.com/"; 22 } 23 } 24 if(bIsIphoneOs || bIsAndroid){ 25 var sUrl = 26 location.href; 27 if(!bForcepc){ 28 window.location.href = "http://smart.mail.163.com/"; 29 } 30 } 31 if(bIsMidp||bIsUc7||bIsUc||bIsCE||bIsWM){ 32 var sUrl = 33 location.href; 34 if(!bForcepc){ 35 window.location.href = "http://m.mail.163.com/"; 36 } 37 } 38 } 39 function 40 fGetQuery(name){//获取参数值 41 var sUrl = window.location.search.substr(1); 42 var r = sUrl.match(new RegExp("(^|&)" + name + 43 "=([^&]*)(&|$)")); 44 return (r == null ? null : (r[2])); 45 } 46 function 47 fShowVerBlock(){ 48 if(bForcepc){ 49 document.getElementByIdx_x("dv_block").style.display = "block"; 50 } 51 else{ 52 document.getElementByIdx_x("ad_block").style.display = "block"; 53 } 54 } 55 fBrowserRedirect(); 56 </script>