【问题标题】:JavaScript - how to detect if the Custom URL scheme is available or not available?JavaScript - 如何检测自定义 URL 方案是否可用?
【发布时间】:2014-08-25 15:08:23
【问题描述】:

在 Windows 操作系统中,我有一个自定义的 URI 方案,用于

IE、火狐、Opera、Safari、谷歌浏览器

启动瞻博网络路由器 VPN SSH 客户端(如 Cisco)。如果安装了 SSH 客户端,基本上它的工作原理如下,从网页 VPN SSH 客户端可以启动。

<a href="juniper:open"> VPN SSH Client </a>

问题:

有时用户没有从 CD/DVD 盒中安装 Juniper 路由器 SSH 客户端应用程序,因此 juniper:open 什么也不做。

所以在这种情况下,我需要检测天气或 URL 方案是否可用。

因此,我尝试了 Javascript 方法,但它无法正常工作。因为 juniper:open 实际上不是网络链接。

请问我该如何检测呢?

<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>

然后:

<a onclick="test1()">TEST 1</a>
<a href="juniper:open" onclick="test2(this.href);return false;">TEST 2</a>

【问题讨论】:

标签: javascript internet-explorer google-chrome firefox url-scheme


【解决方案1】:

编辑 cmets中的以下建议:

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);

}

<a href="javascript:" onclick="goto('juniper:open', 'https://www.junper-affiliate.com/setup.zip');">TEST 2</a> 

您必须支付的价格是对页面的重复请求。

编辑

对于同源策略来说,这是一个很好的解决方法,它可以防止使用 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;
    }
}

编辑

下面的初始解决方案实际上不起作用,因为如果协议不受支持,则不会引发异常。

  try {
    document.location = 'juniper:open';
  } catch (e) {
    document.location = 'https://www.junper-affiliate.com/setup.zip';
  }

【讨论】:

  • 如果使用 catch 而不是 finally 会怎样?你可以试试吗?
  • 如果我使用catch,那么juniper:open 可以工作,但是当我从系统中卸载juniper:open 时,它根本不会退回到junper-affiliate.com/setup.zip
  • 你不能试试这样的东西吗? var script = document.createElement('script'); script.setAttribute('type', 'text/javascript'); script.setAttribute('src', 'doesnotexist.js'); script.onerror = function() { alert("Loading failed!"); } document.getElementsByTagName('head')[0].appendChild(script);
  • 我明白了......你是对的,没有抛出异常。我想出了一个替代方案
  • 查看这个答案:stackoverflow.com/a/22055638/1676126 获得跨浏览器支持有点复杂,但也许可行?
【解决方案2】:

经过大量搜索后,我没有找到任何可以帮助解决我的问题的东西。但这就是我现在正在做的

1) 编写一个小型网络服务器,它可以在 PC 上 24/7 开机、崩溃时运行。使用原生 python:

python -m SimpleHTTPServer [port]

2) 微型网络服务器会监听端口异常,例如 10001 或从未使用过的 TCP 端口

3) 在浏览器中,我们必须与http://localhost:10001 通信才能获得回复

4) 根据该回复,我们必须做出决定

不然好像没办法了

编辑:或者你可以这样做:InnoSetup - Is there any way to manually create cookie for Internet explorer?

【讨论】:

  • 这适用于 Chrome,但不适用于 IE 或 Firefox。他们必须是localhost,并且使用 HTTPs 会引发一系列其他问题
猜你喜欢
  • 1970-01-01
  • 2011-01-20
  • 2015-10-30
  • 2011-02-12
  • 1970-01-01
  • 2010-10-12
  • 1970-01-01
  • 2012-04-22
相关资源
最近更新 更多