Javascript - 检测操作系统并重定向到已安装的移动应用

时间:2015-01-12 21:09:11

标签: javascript ios iphone mobile-application browser-detection

在网页上,我想将用户重定向到应用程序,如果它安装在手机上,如果没有,则重定向到其他网址。例如:

我在下面附上了我的代码。它的工作方式与Windows手机一样。但是对于iPhone,如果未安装该应用程序,则会弹出一条消息“Safari无法打开该页面”。在被重定向到redirectURL之前。有没有办法避免这种或更好的方法来实现它?

到目前为止我的代码:

<head>
<script type="text/javascript">
    function myHref() {

        if (navigator.userAgent.match(/Windows Phone [1-8]/i)) {
            location.href = 'app://url1';
        }

        if (navigator.userAgent.match(/iPad/i) || (navigator.userAgent.match(/iPhone/i))) {

            document.location = 'app://url2';
            setTimeout(function () {
                if (confirm('You do not seem to have the app installed. Do you want to go download it now?')) {
                    location.href = 'https://itunes.apple.com/app/123456';
                }
            }, 300);

        }

        if (navigator.userAgent.match(/Android/i)) {
            location.href = "app://url3"
        }

        if (navigator.userAgent.match(/BlackBerry/i)) {
            //code for blackberry here
        }

        if (navigator.userAgent.match(/webOS/i)) {
            //code for webOS here 
        }
    }
</script>
</head>
<body>
<p>
    <a href="#" onclick="myHref()" style="font-size:20px;">link</a>
</p>
</body>

0 个答案:

没有答案