1

I have found various ways in javascript to detect custom protocols in various browsers i.e., Chrome, FF, IE and Edge. But I haven't been able to found any bit that works for Safari browser. Can anyone help me out with this?

This is not a duplicate question, even if it seems so. All the known solutions don't cater to the Safari browser for MAC. I am wanting this particular solution. Thanks in advance!

  • 1
    Possible duplicate of [How to detect browser's protocol handlers?](http://stackoverflow.com/questions/836777/how-to-detect-browsers-protocol-handlers) – carmine Mar 30 '17 at 07:13
  • This is not a duplicate. There's no working code snippet available for Safari anywhere. – Diparati Sen Apr 03 '17 at 10:14
  • You should search better... starting right from that question, I found the solution I post as answer. – carmine Apr 03 '17 at 13:43

1 Answers1

0

At https://gist.github.com/rajeshsegu/3716941 you can find the gist for browser detection. Despite it does not contains the Safari block, you can simply duplicate the Chrome ones, it works like a charm.

For the lazy ones, here the function:

function launchSafari(){

    var url = getUrl(),
        protcolEl = $('#protocol')[0];

    isSupported = false;


    protcolEl.focus();
    protcolEl.onblur = function(){
        isSupported = true;
        console.log("Text Field onblur called");
    };

    //will trigger onblur
    location.href = url;

    //Note: timeout could vary as per the browser version, have a higher value
    setTimeout(function(){
        protcolEl.onblur = null;
        result()
    }, 500);   

}

This is where it is called:

//Handle Click on Launch button
$('#launch').click(function(){
    if($.browser.mozilla){
        launchMozilla();
    }else if($.browser.chrome){
        launchChrome();
    }else if($.browser.msie){
        launchIE();
    }else if($.browser.safari){
        launchSafari();
    }
});

PS: I'm not a JS expert. Maybe someone could improve the JS code

carmine
  • 1,597
  • 2
  • 24
  • 33
  • This question has nothing to do with "How to detect a browser" - The question is about "How do browsers detect protocols" (https://developer.mozilla.org/en-US/docs/Web/API/Navigator/registerProtocolHandler/Web-based_protocol_handlers) - it is just ignorance that Safari seems not to support it – sebilasse Jan 10 '18 at 15:26