Javascript-检测操作系统并重定向到已安装的移动应用程序
在网页上,如果用户的手机上安装了应用程序,我想将其重定向到另一个URL。例如:Javascript-检测操作系统并重定向到已安装的移动应用程序,javascript,ios,iphone,mobile-application,browser-detection,Javascript,Ios,Iphone,Mobile Application,Browser Detection,在网页上,如果用户的手机上安装了应用程序,我想将其重定向到另一个URL。例如: 电话:Windows,url:app://url1 电话:iPhone,网址:app://url2,重定向URL: 手机:安卓,网址:app://url3,重定向URL: 我在下面附上了我的代码。它在Windows phone上的工作与预期的一样。但是对于iPhone,如果没有安装该应用程序,则会弹出一条消息“Safari无法打开页面”,然后再重定向到重定向URL。有没有办法避免这种情况,或者有更好的方法来实现这
- 电话:Windows,url:app://url1
- 电话:iPhone,网址:app://url2,重定向URL:
- 手机:安卓,网址:app://url3,重定向URL:
<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>
函数myHref(){
if(navigator.userAgent.match(/windowsphone[1-8]/i)){
location.href=app://url1';
}
if(navigator.userAgent.match(/iPad/i)| |(navigator.userAgent.match(/iPhone/i))){
document.location=app://url2';
setTimeout(函数(){
如果(确认('您似乎没有安装应用程序。是否立即下载?')){
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)){
//这里是黑莓的代码
}
if(navigator.userAgent.match(/webOS/i)){
//这里是webOS的代码
}
}