JavaScript-如何检测自定义URL方案是否可用?
在Windows操作系统中,我有一个自定义URI方案,该方案从 IE、Firefox、Opera、Safari、谷歌Chrome 启动Juniper路由器VPN SSH客户端(如Cisco)。基本上,如果安装了SSH客户端,则可以从网页启动VPN SSH客户端JavaScript-如何检测自定义URL方案是否可用?,javascript,internet-explorer,google-chrome,firefox,url-scheme,Javascript,Internet Explorer,Google Chrome,Firefox,Url Scheme,在Windows操作系统中,我有一个自定义URI方案,该方案从 IE、Firefox、Opera、Safari、谷歌Chrome 启动Juniper路由器VPN SSH客户端(如Cisco)。基本上,如果安装了SSH客户端,则可以从网页启动VPN SSH客户端 <a href="juniper:open"> VPN SSH Client </a> 问题: 有时,用户没有从CD/DVD盒安装Juniper路由器SSH客户端应用程序,因此Juniper:open什么也不
<a href="juniper:open"> VPN SSH Client </a>
问题:
有时,用户没有从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>
//失败
函数test1(){
window.location='juniper:open';
setTimeout(函数(){
如果(确认('Missing.Download?')){
document.location=https://www.junper-affiliate.com/setup.zip';
}
}, 25);
//document.location='juniper:open';
}
//失败
功能测试2(h){
文件位置=h;
var time=(新日期()).getTime();
setTimeout(函数(){
var now=(new Date()).getTime();
如果((现在时间)编辑
在评论中提出以下建议:
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);
}
及
编辑
下面的初始解决方案实际上不起作用,因为如果协议不受支持,则不会引发异常
try {
document.location = 'juniper:open';
} catch (e) {
document.location = 'https://www.junper-affiliate.com/setup.zip';
}
在寻找了很多之后,我还没有找到任何能帮助我解决问题的方法。但这就是我现在正在做的
1) 编写一个小型的微型Web服务器,它可以在启动时、崩溃时在PC上全天候运行。使用本机python:
python -m SimpleHTTPServer [port]
2) 微型Web服务器将侦听端口异常,如10001或从未使用过的TCP端口
3) 我们必须通过浏览器与http://localhost:10001
只是为了得到回复
4) 根据这一答复,我们必须作出决定
否则就没有办法了
编辑:或者你可以这样做:如果你使用catch而不是finally怎么办?你能试试吗?如果我使用catch
,那么juniper:open
可以工作,但是当我从系统中卸载juniper:open
时,它就不会退回到原来的版本了。你不能尝试类似的东西吗?var script=document.createElement('script');script.setAttribute('type','text/javascript');script.setAttribute('src','doesnotexist.js');script.onerror=function(){alert(“加载失败!”;}document.getElementsByTagName('head')[0]。appendChild(脚本)
我明白了…你是对的,没有引发任何异常。我提出了另一种选择。检查这个答案:获得跨浏览器支持有点复杂,但可能有效?使用iframe;明白。这在Chrome中有效,但在IE或Firefox中无效。它们必须是这样,而使用HTTPs会引发其他许多问题
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;
}
}
try {
document.location = 'juniper:open';
} catch (e) {
document.location = 'https://www.junper-affiliate.com/setup.zip';
}
python -m SimpleHTTPServer [port]