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

WBOY
Release: 2016-06-06 20:06:40
Original
962 people have browsed it

用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>
Copy after login
Copy after login

回复内容:

用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>
Copy after login
Copy after login

呃,我之前判断过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>
Copy after login

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. ; ↩
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!