js判断浏览器是android还是ios还是微信浏览器

来源:互联网 发布:汽车租赁java 编辑:程序博客网 时间:2024/05/17 07:44

项目中需要扫描二维码之后自动分辨出是Android还是iOS系统,针对于不同的系统进行不同的下载

http://blog.csdn.net/xjtarzan/article/details/46729829

方法一:JS判断是否移动端浏览器

使用这方法既简单,又实用,不需要引入jQuery库,把以下代码加入到<head>里即可。

<script type=”text/javascript”>    if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {        window.location ="mobile.html"; //可以换成http地址    }</script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

或者用下边这段:

if(navigator.userAgent.match(/(iPhone|iPod|iPad|ios|Android)/i)){  window.location ="mobile.html";}
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

方法二:自己写js文件判断 移动端浏览器

    //判断浏览器内核类型    var browser={        versions:function(){            var u = navigator.userAgent, app = navigator.appVersion;            return { //移动终端浏览器版本信息                trident: u.indexOf('Trident') > -1, //IE内核                presto: u.indexOf('Presto') > -1, //opera内核                webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核                gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核                mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端                ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端                android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器                iPhone: u.indexOf('iPhone') > -1 , //是否为iPhone或者QQHD浏览器                iPad: u.indexOf('iPad') > -1, //是否iPad                webApp: u.indexOf('Safari') == -1 //是否web应该程序,没有头部与底部            };        }(),        language:(navigator.browserLanguage || navigator.language).toLowerCase()    }    //跳转链接    if (browser.versions.ios || browser.versions.iPhone || browser.versions.iPad) {         window.location="https://itunes.apple.com/xxx";     }     else if (browser.versions.android) {        window.location="http://xxx/xxx.apk";     }     //document.writeln("语言版本: "+browser.language);    //document.writeln(" 是否为移动终端: "+browser.versions.mobile);    //document.writeln(" ios终端: "+browser.versions.ios);    //document.writeln(" android终端: "+browser.versions.android);    //document.writeln(" 是否为iPhone: "+browser.versions.iPhone);    //document.writeln(" 是否iPad: "+browser.versions.iPad);    //document.writeln(navigator.userAgent);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

方法三:使用 Device.Js 库

device.js 是一个用于检查设备用的插件,使用它你可以很方便的判断设备的操作系统,以及设备是纵向还是横向。首先,我们下载Device.js

下载地址:https://github.com/matthewhudson/device.js

STEP 1: 引入 JS 文件

<script src=”device.min.js”></script>
  • 1
  • 1

STEP 2: 加入判断代码

<script type=”text/javascript”>if(device.mobile()){window.location = “shouji.html”;  //可以换成http地址}</script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

以下是可供使用的设备类型参数:

这里写图片描述

方法四:判断是否微信浏览器

<script type="text/javascript">//手机端判断各个平台浏览器及操作系统平台function checkPlatform(){  if(/android/i.test(navigator.userAgent)){      document.write("This is Android'browser.");//这是Android平台下浏览器  }  if(/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)){      document.write("This is iOS'browser.");//这是iOS平台下浏览器  }  if(/Linux/i.test(navigator.userAgent)){      document.write("This is Linux'browser.");//这是Linux平台下浏览器  }  if(/Linux/i.test(navigator.platform)){      document.write("This is Linux operating system.");//这是Linux操作系统平台  }  if(/MicroMessenger/i.test(navigator.userAgent)){      document.write("This is MicroMessenger'browser.");//这是微信平台下浏览器  }}</script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

或者只需要判断微信浏览器

function isWeiXin(){    var ua = window.navigator.userAgent.toLowerCase();    if(ua.match(/MicroMessenger/i) == 'micromessenger'){         return true;     }    else{        return false;     }}
原创粉丝点击