JavaScript - how to detect if the Custom URL scheme is available or not available?

风格不统一 提交于 2019-11-28 08:34:52

EDIT Following suggestions in comments:

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

}

and

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

The price you have to pay, is a duplicated request for the page.

EDIT

This is a good workaround for same-origin policy, which prevents an asynch version using XMLHTTPRequest to work properly, since SOP restricts cross-domain requests to http and juniper:open would therefore always fail.

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

EDIT

The initial solution below doesn't actually work 'cause no exception is being thrown if the protocal is not supported.

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

After searching a lot i have not came to anything which can help to solve my problem. But this is what i am now making

1) Write a small tiny webserver which can run in the PC for 24/7 on boot, on crash. Use native python:

python -m SimpleHTTPServer [port]

2) tiny webserver will listen on port abnormal such as 10001 or such TCP ports which never get used

3) from Browser we have to communicate with the http://localhost:10001 just to get a reply

4) based on that reply we have to decide

Otherwise there is no way seems to be available

EDIT: Or you can do this way: InnoSetup - Is there any way to manually create cookie for Internet explorer?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!