当用户单击带有自定义协议(如myapp://superlink
)的链接时
我需要启动应用程序或允许用户下载并运行配置应用程序
我正在寻找跨浏览器的方式来检查自定义协议是否已注册
我试图通过检查用户代理服务器端(对于IE)来确定这一点
应用程序设置\5.0\用户代理\发布平台"myapp"="“
发送
`....NET CLR 3.0.30729; .NET4.0C; .NET4.0E; InfoPath.3; **myapp**`
作为用户代理
这是一种很好的,干净的方式,很容易配置:
只需下载.reg文件并运行它或通过ms windows策略传播
我无法为Chrome和Firefox修复此问题
有没有客户端的解决方案( js)?
我的环境: IE8+、Chrome (最新)、火狐(最新)
发布于 2014-08-29 10:21:08
有一个老把戏,它总是会让我失望。
您需要的核心功能是setTimeout
。我会详细地告诉你:
setTimeout(function() {
window.location = "http://itunes.com/app/yourapplocation";
}, 200);
// once you do the custom-uri, it should properly execute the handler, otherwise, the settimeout that you set before will kick in
window.location = "myapp://superlink";
现在你提到它可能是一个或多个链接,所以我做了这个很好的函数,只是为了方便你:
HTML代码
<a href="myapp://superlink" data-href-alt="http://itunes.com/app/yourapplocation">Click here</a>
JS代码
$("a[href*='myapp://']").click(function(e)
{
var el = $(this);
setTimeout(function() {
window.location = el.data("data-href-alt");
}, 200);
// once you do the custom-uri, it should properly execute the handler, otherwise, the settimeout that you set before will kick in
window.location = el.data("href");
e.preventDefault();
});
希望本文能对您有所帮助:)
发布于 2015-03-12 04:31:19
我遇到了一个类似的问题,我需要检查自定义协议是否已经注册(这将打开一个可执行文件),或者打开一个下载页面或执行其他操作。不幸的是,没有简单的方法来解决这个问题,因为每个浏览器的行为都不同。我试着收集了所有的信息,并提出了一个相当通用的库,你可以看看:
https://github.com/ismailhabib/custom-protocol-detection
ps:非Windows8 IE的解决方案相当难看,但我找不到更好的解决方案。
发布于 2015-09-03 00:40:33
由于某些原因,kororo的解决方案对我不起作用,所以我使用了这个稍微修改过的解决方案。
<html>
<a id="link">Click Me</a>
<script>
var link = document.getElementById('link');
var timeout;
window.addEventListener('blur',function(e){
window.clearTimeout(timeout);
})
link.addEventListener('click', function(e) {
timeout = window.setTimeout(function() {
console.log('timeout');
window.location = "https://myapp.net";
}, 1000);
window.location = "myapp://";
e.preventDefault();
});
</script>
</html>
https://stackoverflow.com/questions/24779312
复制相似问题