10 javascript firefox internet-explorer google-chrome url-scheme
在Windows操作系统中,我有一个自定义URI方案,使用它
IE,Firefox,Opera,Safari,Google Chrome
推出Juniper路由器VPN SSH客户端(如思科).基本上,如果安装了SSH客户端,它的工作原理如下,从网页上可以启动VPN SSH Client.
<a href="juniper:open"> VPN SSH Client </a>
Run Code Online (Sandbox Code Playgroud)
问题:
有时用户没有从CD/DVD盒安装Juniper路由器SSH客户端应用程序,因此juniper:open什么都不做.
所以在这种情况下,我需要检测天气是否可用URL方案.
因此,我尝试了Javascript方法,但它没有完全正常工作.因为juniper:open实际上不是web链接.
我如何检测它呢?
<script>
// Fails
function test1(){
window.location = 'juniper:open';
setTimeout(function(){
if(confirm('Missing. Download it now?')){
document.location = 'https://www.junper-affiliate.com/setup.zip';
}
}, 25);
//document.location = 'juniper:open';
}
// Fails
function test2(h){
document.location=h;
var time = (new Date()).getTime();
setTimeout(function(){
var now = (new Date()).getTime();
if((now-time)<400) {
if(confirm('Missing. Download it now?')){
document.location = 'https://www.junper-affiliate.com/setup.zip';
} else {
document.location=h;
}
}
}, 300);
}
</script>
Run Code Online (Sandbox Code Playgroud)
然后:
<a onclick="test1()">TEST 1</a>
<a href="juniper:open" onclick="test2(this.href);return false;">TEST 2</a>
Run Code Online (Sandbox Code Playgroud)
编辑 以下评论中的建议:
function goto(url, fallback) {
var script = document.createElement('script');
script.onload = function() {
document.location = url;
}
script.onerror = function() {
document.location = fallback;
}
script.setAttribute('src', url);
document.getElementsByTagName('head')[0].appendChild(script);
}
Run Code Online (Sandbox Code Playgroud)
和
<a href="javascript:" onclick="goto('juniper:open', 'https://www.junper-affiliate.com/setup.zip');">TEST 2</a>
Run Code Online (Sandbox Code Playgroud)
您必须支付的价格是页面的重复请求.
编辑
这是同源策略的一个很好的解决方法,它可以防止使用XMLHTTPRequest的异步版本正常工作,因为SOP将跨域请求限制为http和juniper:open因此总是会失败.
function goto(url, fallback) {
var xmlhttp=new XMLHttpRequest();
xmlhttp.open('GET', url, false);
try {
xmlhttp.send(null); // Send the request now
} catch (e) {
document.location = fallback;
return;
}
// Throw an error if the request was not 200 OK
if (xmlhttp.status === 200) {
document.location = url;
} else {
document.location = fallback;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑
下面的初始解决方案实际上不起作用'因为如果不支持协议,则不会抛出任何异常.
try {
document.location = 'juniper:open';
} catch (e) {
document.location = 'https://www.junper-affiliate.com/setup.zip';
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15340 次 |
| 最近记录: |