码迷,mamicode.com
首页 > 移动开发 > 详细

通过js判断手机访问跳转到手机站

时间:2015-03-04 18:59:21      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

  1. 第一种方法:

    <script>
    
        if(navigator.platform.indexOf(Win32)!=-1){ 
    
        //pc
    
            //window.location.href="电脑网址";
    
         }else{ 
    
       //shouji
    
            window.location.href="手机网址";
    
          } 
    
    </script>

     

  2. 第二种方法:

    <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= 手机网站地址; 
    
    } else { 
    
    window.location= 电脑网站地址; 
    
    } 
    
    } 
    
    browserRedirect(); 
    
    </script>

     

  3. 第三种方法:

    <script type="text/javascript">
    
    var mobileAgent = new Array("iphone", "ipod", "ipad", "android", "mobile", "blackberry", "webos", "incognito", "webmate", "bada", "nokia", "lg", "ucweb", "skyfire");
    
    var browser = navigator.userAgent.toLowerCase(); 
    
    var isMobile = false; 
    
    for (var i=0; i<mobileAgent.length; i++){ if (browser.indexOf(mobileAgent[i])!=-1){ isMobile = true; 
    
    //alert(mobileAgent[i]); 
    
    location.href = 手机网址;
    
    break; } } 
    
    </script>

     

  4. 第四种方法:

    <?php
    
    ////这段放手机站里
    
    header("Cache-Control: no-cache"); 
    
    header("Pragma: no-cache");
    
    $ua = strtolower($_SERVER[HTTP_USER_AGENT]);
    
    $uachar = "/(symbianos|android|Mac OS|ucweb|blackberry)/i";
    
    if($ua != ‘‘ && !preg_match($uachar, $ua)){
    
    echo window.location.href="电脑地址";;
    
    exit();
    
    }
    
    ?>
    
    <?php
    
    //这段放在网站里
    
    header("Cache-Control: no-cache"); 
    
    header("Pragma: no-cache");
    
    $ua = strtolower($_SERVER[HTTP_USER_AGENT]);
    
    $uachar = "/(symbianos|android|Mac OS|ucweb|blackberry)/i";
    
    if($ua != ‘‘ && preg_match($uachar, $ua)){
    
    echo window.location.href="手机地址";;
    
    exit();
    
    }
    
    ?>

     

  5. 第五种方法:

    <?php
    
    function UserAgent(){
    
        $user_agent = ( !isset($_SERVER[HTTP_USER_AGENT])) ? FALSE : $_SERVER[HTTP_USER_AGENT];
    
        return $user_agent;
    
    }
    
    //Mobile
    
    if ((preg_match("/(iphone|ipod|android)/i", strtolower(UserAgent()))) AND strstr(strtolower(UserAgent()), webkit)){
    
        header(Location: 手机网址);
    
        exit;
    
    }else if(trim(UserAgent()) == ‘‘ OR preg_match("/(nokia|sony|ericsson|mot|htc|samsung|sgh|lg|philips|lenovo|ucweb|opera mobi|windows mobile|blackberry)/i", strtolower(UserAgent()))){
    
        header(Location: 手机网址);
    
        exit;
    
    }else{//PC
    
        header("Location: 电脑网址");
    
    }
    
    ?>

     

  6. 第六种方法,也可以把名为uaredirect的文件下载到自己网站上引入;

    技术分享

     (说明:把uaredirect放在百度中搜下就出现文件具体地址了)

  • 说明:个人建议是用百度提供的方法,简单,方便,对网站打开速度影响小

通过js判断手机访问跳转到手机站

标签:

原文地址:http://www.cnblogs.com/WZH75171992/p/4314003.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!