javascript 我可以制作一个链接,如果安装了它,它会在我的应用程序中打开,如果没有,我可以回退到不同的 URL 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11710902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I make a link that will open in my app if it's installed, and fall back to a different URL if not?
提问by Simon
Users can share files from our app via twitter. The tweet includes a URL that points at our server, which detects whether the user is on a mobile device and redirects to a URL using our app's custom scheme so that the link opens in our app.
用户可以通过 Twitter 从我们的应用程序共享文件。该推文包含一个指向我们服务器的 URL,它检测用户是否在移动设备上,并使用我们应用程序的自定义方案重定向到一个 URL,以便在我们的应用程序中打开链接。
This works fine for desktop users, and for mobile users who have our app installed; but it doesn't work for mobile users who don't. So what we'd like to do instead is to show all users a page that contains a link which, when pressed, will open the app with the custom URL scheme if it is supported, and open a different URL where the user can download our app if not.
这适用于桌面用户和安装了我们应用程序的移动用户;但它不适用于不这样做的移动用户。因此,我们想要做的是向所有用户显示一个包含链接的页面,当按下该链接时,将使用自定义 URL 方案(如果支持)打开应用程序,并打开一个不同的 URL,用户可以在其中下载我们的应用程序如果没有。
So what I'm looking for is an answer in HTML or JS that looks a bit like this:
所以我要寻找的是 HTML 或 JS 中的答案,看起来有点像这样:
<a href="ourapp://www.ourdomain.com/files/12345"
fallbackhref="http://www.ourdomain.com/buyourapp">Click to download</a>
Is that possible? If so, how do we do it?
那可能吗?如果是这样,我们该怎么做?
回答by Akshat Singhal
You can achieve this in Android using the following piece of code :
您可以使用以下代码在 Android 中实现此目的:
function openLink () {
var appWindow = window.open("ourapp://www.ourdomain.com/files/12345","_blank");
setTimeout( function () {if (appWindow) {
appWindow.location ="http://www.ourdomain.com/buyourapp";
}
},1000);
}
Call the openLink()
function on click of a link (else the browser will block the new window as popup).
openLink()
单击链接时调用该函数(否则浏览器将阻止新窗口作为弹出窗口)。
iOS would differ because of the way it handles custom schemes.
iOS 会因其处理自定义方案的方式而有所不同。
For iOS you need to do the following : Create 2 HTML files with the following code snippets
对于 iOS,您需要执行以下操作:使用以下代码片段创建 2 个 HTML 文件
File #1 : This is your link to open the app/ fallback to website
文件 #1:这是您打开应用程序/回退到网站的链接
<script type="text/javascript">
function openLink (url,customURI) {
window.open("file2.html?lp="+url+"&customURI="+customURI,"_blank");
}
</script>
<img src="IMAGE SOURCE" onclick="openLink('LANDING PAGE','CUSTOM URI')">
File #2 :
文件#2:
<html>
<script>
function openApp () {
var start, end, elapsed;
var params = window.location.href.split("lp=")[1];
var url = params.split("&customURI=")[0];
var customURI = params.split("&customURI=")[1];
var time = (new Date()).getTime();
document.location = customURI+url;
setTimeout(function(){
var now = (new Date()).getTime();
if((now - time)<2550) {
javascript: console.log(new Date().getTime());
document.location = url;
}
else{
window.open('', '_self', '');
window.close();
}
}, 2000);
}
</script>
<body onload="openApp()">
<a onclick="openApp()" id="newButton">Click Here to open the App</a>
</body>
Hope this helps :)
希望这可以帮助 :)