一個二維碼判斷是否是安卓和ios或微信,分別跳轉下載頁

方法1:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

    <script>
     toDownload(); 
        // 新的方法
        function toDownload(){ 
            var u = navigator.userAgent;
            var ua = u.toLowerCase();   
            if (/iphone|ipad|ipod/.test(ua)) {  // iOS 系統 ->  跳AppStore下載地址
                alert("iOS"); 
                //window.location.href = 'https://itunes.apple.com/cn/app/xxxxxxx/id1124348115?mt=8';
            } else if ( /android/.test(ua)) { // 安卓系統 -> 跳安卓端下載地址
                //window.location.href = 'http://xxxxxxx.cn/release/xxxx-release.apk';
                alert("android"); 
            } else if (IsPC()){ // PC端
								alert("pc");
            }
        }
        </script>
        </body>
</html>

方法2:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

    <script>
    goDownload(); 
        function goDownload() {
            var u = navigator.userAgent, app = navigator.appVersion;
            var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1;
            var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
            // 是安卓瀏覽器
            if (isAndroid) {
               // window.location.href = 'http://xxxxxxx.cn/release/xxxx-release.apk'; // 跳安卓端下載地址
               alert("android");
            }
            // 是iOS瀏覽器
            if (isIOS) {
               // window.location.href = 'https://itunes.apple.com/cn/app/xxxxxxx/id1124348115?mt=8'; // 跳AppStore下載地址
               alert("ios");
            }

            // 是微信內部webView
            if (is_weixn()) {
                alert("wx");
            }

            // 是PC端
            if (IsPC()) {
               // window.location.href = 'http://www.xxxxxxx.cn/index.html';  // 公司主頁
                alert("pc");
            }
        }
        // 是微信瀏覽器
        function is_weixn(){
            var ua = navigator.userAgent.toLowerCase();
            if(ua.match(/MicroMessenger/i)=="micromessenger") {
                return true;
            } else {
                return false;
            }
        }
      

        function IsPC() {
            var userAgentInfo = navigator.userAgent;
            var Agents = ["Android", "iPhone",
                "SymbianOS", "Windows Phone",
                "iPad", "iPod"];
            var flag = true;
            for (var v = 0; v < Agents.length; v++) {
                if (userAgentInfo.indexOf(Agents[v]) > 0) {
                    flag = false;
                    break;
                }
            }
            return flag;
        }

    </script>
    </body>
</html>

參考鏈接:https://www.jianshu.com/p/f9db096857c2

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章