JS 判断设备 浏览器

话不多说,直接看代码

 

1、区分Android、iphone、ipad:

  1. var ua = navigator.userAgent.toLowerCase();
  2. if (/android|adr/gi.test(ua)) {
  3. // 安卓
  4. }else if(/\(i[^;]+;( U;)? CPU.+Mac OS X/gi.test(ua)){
  5. //苹果
  6. }else if(/iPad/gi.test(ua)){
  7. //ipad
  8. }

2、区分设备:  新浪微博为1,QQ客户端为2,微信低于6.0.2版本为3,高于6.0.2版本为4,其他为0。

  1. var ua = navigator.userAgent.toLowerCase();
  2. if(ua.match(/weibo/i) == "weibo"){
  3. console.log(1);
  4. }else if(ua.indexOf(\'qq/\')!= -1){
  5. console.log(2);
  6. }else if(ua.match(/MicroMessenger/i)=="micromessenger"){
  7. var v_weixin = ua.split(\'micromessenger\')[1];
  8. v_weixin = v_weixin.substring(1,6);
  9. v_weixin = v_weixin.split(\' \')[0];
  10. if(v_weixin.split(\'.\').length == 2){
  11. v_weixin = v_weixin + \'.0\';
  12. }
  13. if(v_weixin < \'6.0.2\'){
  14. console.log(3);
  15. }else{
  16. console.log(4);
  17. }
  18. }else{
  19. console.log(0);
  20. }

3、区分各个浏览器

  1. var ua=navigator.userAgent.toLowerCase();
  2. if(/msie/i.test(ua) && !/opera/.test(ua)){
  3. alert("IE");
  4. return ;
  5. }else if(/firefox/i.test(ua)){
  6. alert("Firefox");
  7. return ;
  8. }else if(/chrome/i.test(ua) && /webkit/i.test(ua) && /mozilla/i.test(ua)){
  9. alert("Chrome");
  10. return ;
  11. }else if(/opera/i.test(ua)){
  12. alert("Opera");
  13. return ;
  14. }else if(/iPad/i){
  15. alert("ipad");
  16. return ;
  17. }
  18. else if(/webkit/i.test(ua) &&!(/chrome/i.test(ua) && /webkit/i.test(ua) && /mozilla/i.test(ua))){
  19. alert("Safari");
  20. return ;
  21. }else{
  22. alert("unKnow");
  23. }

版权声明:本文为chenhaoyu原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/chenhaoyu/p/9247217.html