Home  >  Article  >  Backend Development  >  javascript - 用JS如何判断打开网页的是安卓还是ios

javascript - 用JS如何判断打开网页的是安卓还是ios

WBOY
WBOYOriginal
2016-06-06 20:06:401003browse

用js如何判断打开网页的是安卓还是ios?

我要做个html页面,有个链接下载app,如果是苹果手机打开就下载ios版安卓就下载安卓版,
如何用js区分?

<code><script type="text/javascript">
$(function(){
    var ua = navigator.userAgent.toLowerCase();
    if (/iphone|ipad|ipod/.test(ua)) {
      $("a").attr("href","https://itunes.apple.com/us/app/ren-ren-duo-bao-yi-ge-shou/id1111430715?l=zh&ls=1&mt=8"); 
    } else {
          $("a").attr("href","http://www.rrdb8.com/statics/templates/app/RenRenDuoBao.apk"); 
    }
})
</script>

做出来了

</code>

回复内容:

用js如何判断打开网页的是安卓还是ios?

我要做个html页面,有个链接下载app,如果是苹果手机打开就下载ios版安卓就下载安卓版,
如何用js区分?

<code><script type="text/javascript">
$(function(){
    var ua = navigator.userAgent.toLowerCase();
    if (/iphone|ipad|ipod/.test(ua)) {
      $("a").attr("href","https://itunes.apple.com/us/app/ren-ren-duo-bao-yi-ge-shou/id1111430715?l=zh&ls=1&mt=8"); 
    } else {
          $("a").attr("href","http://www.rrdb8.com/statics/templates/app/RenRenDuoBao.apk"); 
    }
})
</script>

做出来了

</code>

呃,我之前判断过android和iPhone,直接判断UA,如果是iOS的话,参考其他人的答案

<code>function getOS(){
    var ua = window.navigator.userAgent.toLowerCase(),
        iPhone = /iphone/i.test(ua) ? true : false,
        android = /android/i.test(ua) ? true : false;
    
    return {android:android, iPhone:iPhone};
}</code>

https://github.com/matthewhudson/device.js可以试试这个库

var userAgent = navigator.userAgent;
var isAndroid = userAgent.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
var isiOS = !!userAgent.match(/(i1+;( U;)? CPU.+Mac OS X/); //ios终端

根据isAndroid,isiOS为 1 或者 0 就可以做出判断。


  1. ; ↩
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn