JS前端开发判断是否是手机端并跳转操作(小结)(转)

这篇文章主要介绍了JS前端开发判断是否是手机端并跳转操作,非常不错,具有参考借鉴价值,需要的朋友可以参考下
常用跳转代码
 1 <script type="text/javascript">
 2  // borwserRedirect
 3  (function browserRedirect(){
 4   var sUserAgent = navigator.userAgent.toLowerCase();
 5   var bIsIpad = sUserAgent.match(/ipad/i) == 'ipad';
 6   var bIsIphone = sUserAgent.match(/iphone os/i) == 'iphone os';
 7   var bIsMidp = sUserAgent.match(/midp/i) == 'midp';
 8   var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == 'rv:1.2.3.4';
 9   var bIsUc = sUserAgent.match(/ucweb/i) == 'web';
10   var bIsCE = sUserAgent.match(/windows ce/i) == 'windows ce';
11   var bIsWM = sUserAgent.match(/windows mobile/i) == 'windows mobile';
12   var bIsAndroid = sUserAgent.match(/android/i) == 'android';
13   if(bIsIpad || bIsIphone || bIsMidp || bIsUc7 || bIsUc || bIsCE || bIsWM || bIsAndroid ){
14   window.location.href = '跳转的移动端网址';
15   }
16  })();
17  </script>
18 <script type="text/javascript"> 
19 <!-- 
20   //平台、设备和操作系统 
21   var system = { 
22    win: false, 
23    mac: false, 
24    xll: false, 
25    ipad:false
26   }; 
27   //检测平台 
28   var p = navigator.platform; 
29   system.win = p.indexOf("Win") == 0; 
30   system.mac = p.indexOf("Mac") == 0; 
31   system.x11 = (p == "X11") || (p.indexOf("Linux") == 0); 
32   system.ipad = (navigator.userAgent.match(/iPad/i) != null)?true:false; 
33   //跳转语句,如果是手机访问就自动跳转到wap.baidu.com页面 
34   if (system.win || system.mac || system.xll||system.ipad) { 
35   } else { 
36    window.location.href = "http://www.jdpatro.com/3g/"; 
37   } 
38 --> 
39 </script>

腾讯跳转

 1 <script type="text/javascript">
 2 if(window.location.toString().indexOf('pref=padindex') != -1){
 3 }else{
 4  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))){ 
 5   if(window.location.href.indexOf("?mobile")<0){
 6   try{
 7    if(/Android|Windows Phone|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){
 8     window.location.href="http://xw.qq.com/index.htm";
 9    }else if(/iPad/i.test(navigator.userAgent)){
10     //window.location.href="http://www.qq.com/pad/"
11    }else{
12     window.location.href="http://xw.qq.com/simple/s/index/"
13    }
14   }catch(e){}
15  }
16  }
17 }
18 </script>
19 <script type="text/javascript"> 
20 <!-- 
21   //平台、设备和操作系统 
22   var system = { 
23    win: false, 
24    mac: false, 
25    xll: false, 
26    ipad:false
27   }; 
28   //检测平台 
29   var p = navigator.platform; 
30   system.win = p.indexOf("Win") == 0; 
31   system.mac = p.indexOf("Mac") == 0; 
32   system.x11 = (p == "X11") || (p.indexOf("Linux") == 0); 
33   system.ipad = (navigator.userAgent.match(/iPad/i) != null)?true:false; 
34   //跳转语句,如果是手机访问就自动跳转到wap.baidu.com页面 
35   if (system.win || system.mac || system.xll||system.ipad) { 
36   } else { 
37    window.location.href = "http://www.jdpatro.com/3g/"; 
38   } 
39 --> 
40 </script>

JS 判断浏览器客户端类型(ipad,iphone,android)

 1 <script type="text/javascript"> 
 2  var bForcepc = fGetQuery("dv") == "pc"; 
 3  function fBrowserRedirect(){ 
 4   var sUserAgent = navigator.userAgent.toLowerCase(); 
 5   var bIsIpad = sUserAgent.match(/ipad/i) == "ipad"; 
 6   var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os"; 
 7   var bIsMidp = sUserAgent.match(/midp/i) == "midp"; 
 8   var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4"; 
 9   var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb"; 
10   var bIsAndroid = sUserAgent.match(/android/i) == "android"; 
11   var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce"; 
12   var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile"; 
13   if(bIsIpad){ 
14    var sUrl = location.href;  
15    if(!bForcepc){ 
16     window.location.href = "http://ipad.mail.163.com/"; 
17    } 
18   } 
19   if(bIsIphoneOs || bIsAndroid){ 
20    var sUrl = location.href;  
21    if(!bForcepc){ 
22     window.location.href = "http://smart.mail.163.com/"; 
23    } 
24   } 
25   if(bIsMidp||bIsUc7||bIsUc||bIsCE||bIsWM){ 
26    var sUrl = location.href;  
27    if(!bForcepc){ 
28     window.location.href = "http://m.mail.163.com/"; 
29    } 
30   } 
31  } 
32  function fGetQuery(name){//获取参数值 
33   var sUrl = window.location.search.substr(1); 
34   var r = sUrl.match(new RegExp("(^|&)" + name + "=([^&]*)(&|$)")); 
35   return (r == null ? null : (r[2])); 
36  } 
37  function fShowVerBlock(){  
38   if(bForcepc){ 
39    document.getElementByIdx_x("dv_block").style.display = "block"; 
40   } 
41   else{ 
42    document.getElementByIdx_x("ad_block").style.display = "block"; 
43   } 
44  } 
45  fBrowserRedirect(); 
46  </script>

以上所述是小编给大家介绍的JS前端开发判断是否是手机端并跳转操作,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的,在此也非常感谢大家对脚本之家网站的支持!

posted @ 2017-10-25 10:14  Andy.Qiu  阅读(305)  评论(1编辑  收藏  举报