使用navigator.userAgent.toLowerCase()判斷移動端類型


使用navigator.userAgent.toLowerCase()判斷移動端類型

判斷設備,區分Android,iphone,ipad和其它

var ua   = navigator.userAgent.toLowerCase();   
if(ua.match(/android/i)) == "android")  
{  
    alert("android");  
}  
if(ua.match(/iPhone/i)) == "iPhone")  
{  
    alert("iPhone");  
}  
if(ua.match(/iPad/i)) == "iPad")  
{  
    alert("iPad");  
}  

判斷是不是特定類型的瀏覽器,比如新浪weibo客戶端內置瀏覽器,qq客戶端內置瀏覽器(而非qq瀏覽器),微信內置瀏覽器

(並且區分版本是否大於等於6.0.2)(特定類型瀏覽器可能會存在,無法下載,無法跳轉和自己的客戶端app的特定協議等等,所以需要區分)

(由於微信在6.0.2的時候做了新的策略,使得微信的分享功能在新版本變得不一樣,為了兼容新舊版本,這里做了區分操作)

新浪weibo客戶端返回1,qq客戶端返回2,微信小於6.0.2版本返回3,微信大於等於6.0.2版本返回4,其它返回0

var ua = navigator.userAgent.toLowerCase();  
    if(ua.match(/weibo/i) == "weibo"){  
        return 1;  
    }else if(ua.indexOf('qq/')!= -1){  
        return 2;  
    }else if(ua.match(/MicroMessenger/i)=="micromessenger"){  
        var v_weixin = ua.split('micromessenger')[1];  
        v_weixin = v_weixin.substring(1,6);  
        v_weixin = v_weixin.split(' ')[0];  
        if(v_weixin.split('.').length == 2){  
            v_weixin = v_weixin + '.0';  
        }  
        if(v_weixin < '6.0.2'){  
            return 3;  
        }else{  
            return 4;  
        }  
    }else{  
        return 0;  
    }  

  判斷QQ內置瀏覽器,QQ瀏覽器APP,微信瀏覽器(只含有MQQbrowser的是QQ瀏覽器,含有mobile Mqqbrowser的是QQ內置瀏覽器。)

         if(ua.indexOf(' qq')>-1 && ua.indexOf('mqqbrowser') <0){
              //qq內置瀏覽器
              isQQInstalled = true;
              return;
          }
          if(ua.indexOf('mqqbrowser')> -1 && ua.indexOf(" qq")<0){
              //qq瀏覽器
              isQQ = true;
              return;
          }
          if (ua.match(/MicroMessenger/i) == 'micromessenger') {
             //微信瀏覽器
             isWx = true;
             return;
          }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM