天天看點

JavaScript中浏覽器打開或下載下傳app

浏覽器打開或下載下傳app

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="apple-itunes-app" content="app-id=477927812">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <a id="openApp">點選打開貼吧用戶端</a>
    <p id="mod_pop"style="display:none;">請在其他浏覽器中打開</p>

    <script type="text/javascript">.onload = function () {
            function isWeixin() {
                var ua = navigator.userAgent.toLowerCase(); //擷取判斷用的對象
                if (ua.match(/MicroMessenger/i) == "micromessenger") {
                    return true; //微信打開
                } else {
                    return false; //不是微信打開
                }
            }

            document.getElementById('openApp').onclick = function (e) {
                if (isWeixin()) {
                    var modPop = document.getElementById('mod_pop');
                    modPop.style.display = "block";
                } else {
                    if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { //ios判斷
                        window.location.href = "com.baidu.tieba://"; //通過app打開協定來打開app
                        window.setTimeout(function () {
                            window.location.href = "https://itunes.apple.com/cn/app/id477927812"; //沒有彈框打開app則打開app下載下傳位址
                        }, 20)
                    } else if (/(Android)/i.test(navigator.userAgent)) { //Android判斷
                        var state = null;
                        try {
                            state = window.open("com.baidu.tieba://");
                        } catch (e) {}
                        if (state) {
                            window.close();
                        } else {
                            window.location.href = "https://downpack.baidu.com/baidutieba_AndroidPhone_v9.0.8.0(9.0.8.0)_1019960r.apk";
                        }
                    }
                }
            };
        }</script>
</body>

</html>      

繼續閱讀