javascript 最简单的跨浏览器检查协议处理程序是否已注册
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24779312/
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
simplest cross-browser check if protocol handler is registered
提问by jonny
When user clicks link with custom protocol (like myapp://superlink
)
当用户单击带有自定义协议的链接时(如myapp://superlink
)
I need either launch an app or allow user to download and run configuration app
我需要启动应用程序或允许用户下载和运行配置应用程序
I am looking for cross-browser way to check if custom protocol is registered
我正在寻找跨浏览器的方式来检查自定义协议是否已注册
I've tried to determine this by checking user agent server-side (for IE)
我试图通过检查用户代理服务器端(对于 IE)来确定这一点
[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Internet Settings\5.0\User Agent\Post Platform] "myapp"=""
[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Internet Settings\5.0\User Agent\Post Platform] "myapp"=""
sends
发送
`....NET CLR 3.0.30729; .NET4.0C; .NET4.0E; InfoPath.3; **myapp**`
as user-agent
作为用户代理
This is good, clean way, easy configuration:
这是好的,干净的方式,易于配置:
just download .reg file and run it or propagiate via ms windows policy
只需下载 .reg 文件并运行它或通过 ms windows 策略传播
I can't fix this for Chrome and Firefox
我无法为 Chrome 和 Firefox 修复此问题
Are there any client-side solution (in js)?
是否有任何客户端解决方案(在 js 中)?
My enviroment: IE8+, Chrome (latest), Firefox(latest)
我的环境:IE8+、Chrome(最新)、Firefox(最新)
回答by kororo
There is this old tricks that it always never fails me.
有这个老把戏,它永远不会让我失望。
The core functionality that you need is setTimeout
. I will tell you in detail:
您需要的核心功能是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";
Now you mentioned that it maybe a link or links so I made this nice function just for your convenience:
现在你提到它可能是一个或多个链接,所以我做了这个很好的功能只是为了你的方便:
HTML code
HTML代码
<a href="myapp://superlink" data-href-alt="http://itunes.com/app/yourapplocation">Click here</a>
JS code
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();
});
Hope this will help you :)
希望能帮到你 :)
回答by habsq
I had a similar problem where I needed to check whether a custom protocol is already registered (which will open an executable file), or otherwise open a download page or do something else. Unfortunately there is no easy way to deal with this since every browser behaves differently. I tried to collect all information and come up with a rather generic library for this matter, you can take a look at:
我有一个类似的问题,我需要检查自定义协议是否已经注册(这将打开一个可执行文件),或者打开一个下载页面或做其他事情。不幸的是,没有简单的方法来处理这个问题,因为每个浏览器的行为都不同。我试图收集所有信息并为此问题提出一个相当通用的库,你可以看看:
https://github.com/ismailhabib/custom-protocol-detection
https://github.com/ismailhabib/custom-protocol-detection
ps: the solution for non Windows 8 IE is rather ugly, but I couldn't find a better solution.
ps:非Windows 8 IE的解决方案相当丑陋,但我找不到更好的解决方案。
回答by habsq
kororo's solution wouldn't work for me for some reason, so I managed with this slightly modified solution instead.
出于某种原因,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>
回答by Ron Teitelbaum
Update to Korono's answer, this worked for me:
更新 Korono 的回答,这对我有用:
$(function() {
var timeIndex;
$("a[href*='myapp://']").blur(function(e)
{
clearTimeout(timeIndex);
});
$("a[href*='myapp://']").click(function(e)
{
var el = $(this);
timeIndex = setTimeout(function() {
window.location = el.attr("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.attr("href");
e.preventDefault();
});
});
回答by Ali Jamal
Salaam
萨拉姆
Install ismailhabib/custom-protocol-detection
安装ismailhabib/custom-protocol-detection
By including this JS file protocolcheck.js
通过包含这个 JS 文件protocolcheck.js
Then write code something like this
然后写这样的代码
<div id="protocol" href="myprotocol:[email protected]">Check Protocol</div>
<script>
$("#protocol").click(function (event) {
protocolCheck($(this).attr("href"), function () {
alert("protocol not recognized");
});
event.preventDefault ? event.preventDefault() : event.returnValue = false;
});
</script>