JS 判斷各種設備,各種瀏覽器


話不多說,直接看代碼

 

1、區分Android、iphone、ipad:

var ua = navigator.userAgent.toLowerCase();
if (/android|adr/gi.test(ua)) {
    // 安卓
     
}else if(/\(i[^;]+;( U;)? CPU.+Mac OS X/gi.test(ua)){
    //蘋果
     
}else if(/iPad/gi.test(ua)){
    //ipad
 
}

2、區分設備:  新浪微博為1,QQ客戶端為2,微信低於6.0.2版本為3,高於6.0.2版本為4,其他為0。

var ua = navigator.userAgent.toLowerCase();  
if(ua.match(/weibo/i) == "weibo"){  
    console.log(1);
}else if(ua.indexOf('qq/')!= -1){  
    console.log(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'){  
    console.log(3);
}else{  
    console.log(4);  
}  
}else{  
    console.log(0); 
} 

3、區分各個瀏覽器

 var ua=navigator.userAgent.toLowerCase();  
    if(/msie/i.test(ua) && !/opera/.test(ua)){  
        alert("IE");  
        return ;  
    }else if(/firefox/i.test(ua)){  
        alert("Firefox");  
        return ;  
    }else if(/chrome/i.test(ua) && /webkit/i.test(ua) && /mozilla/i.test(ua)){  
        alert("Chrome");  
        return ;  
    }else if(/opera/i.test(ua)){  
        alert("Opera");  
        return ;  
    }else if(/iPad/i){ 
        alert("ipad"); 
        return ; 
    }
else if(/webkit/i.test(ua) &&!(/chrome/i.test(ua) && /webkit/i.test(ua) && /mozilla/i.test(ua))){  
        alert("Safari");  
        return ;  
    }else{  
        alert("unKnow");  
    }


免責聲明!

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



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