Source:
Created
May 30, 2014 02:06
-
-
Save kfatehi/f2f521c654bab106fdf9 to your computer and use it in GitHub Desktop.
Detect Custom Protocol Handler w/ Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Detect Custome Protocol</title> | |
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.1.min.js"></script> | |
</head> | |
<body> | |
<input id="protocol" value="" placeholder="custom protocol"/> | |
<button id="launch">Detect</button> | |
<!-- Mozilla Only --> | |
<iframe id="hiddenIframe" src="about:blank" style="display:none;"></iframe> | |
<!-- IE Case 1 --> | |
<a id="hiddenLink" style="display:none;" href="#">custom protocol</a> | |
<script> | |
//Default State | |
var isSupported = false; | |
//Helper Methods | |
function getProtocol(){ | |
return $('#protocol').val(); | |
} | |
function getUrl(){ | |
return getProtocol()+"://"+"rajeshsegu.com"; | |
} | |
function result(){ | |
alert(getProtocol() + " supported => " + isSupported); | |
} | |
//Handle Click on Launch button | |
$('#launch').click(function(){ | |
if($.browser.mozilla){ | |
launchMozilla(); | |
}else if($.browser.chrome){ | |
launchChrome(); | |
}else if($.browser.msie){ | |
launchIE(); | |
} | |
}); | |
//Handle IE | |
function launchIE(){ | |
var url = getUrl(), | |
aLink = $('#hiddenLink')[0]; | |
isSupported = false; | |
aLink.href = url; | |
//Case 1: protcolLong | |
console.log("Case 1"); | |
if(navigator.appName=="Microsoft Internet Explorer" | |
&& aLink.protocolLong=="Unknown Protocol"){ | |
isSupported = false; | |
result(); | |
return; | |
} | |
//Case2: Open New Window, set iframe src, and access the location.href | |
console.log("Case 2"); | |
var myWindow = window.open('','','width=0,height=0'); | |
myWindow.document.write("<iframe src='"+ url + "></iframe>"); | |
setTimeout(function(){ | |
try{ | |
myWindow.location.href; | |
isSupported = true; | |
}catch(e){ | |
//Handle Exception | |
} | |
if(isSupported){ | |
myWindow.setTimeout('window.close()', 100); | |
}else{ | |
myWindow.close(); | |
} | |
result(); | |
}, 100) | |
}; | |
//Handle Firefox | |
function launchMozilla(){ | |
var url = getUrl(), | |
iFrame = $('#hiddenIframe')[0]; | |
isSupported = false; | |
//Set iframe.src and handle exception | |
try{ | |
iFrame.contentWindow.location.href = url; | |
isSupported = true; | |
result(); | |
}catch(e){ | |
//FireFox | |
if (e.name == "NS_ERROR_UNKNOWN_PROTOCOL"){ | |
isSupported = false; | |
result(); | |
} | |
} | |
} | |
//Handle Chrome | |
function launchChrome(){ | |
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; | |
setTimeout(function(){ | |
protcolEl.onblur = null; | |
result() | |
}, 300); | |
} | |
</script> | |
</body> | |
</html> |
Does not seem to work with new browser versions anymore. In Chrome and Edge isSupported
is always false
, even after an application for an URI scheme was successfully opened. In Firefox isSupported
is always true
, even for unknown URI schemes.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried in chrome with many application like msteams, skype, github etc but all are opening on click of Detect button but in alert it is showing Support = False. Logically it should show support = true when application is available and protocol is registered with browser. Please correct me if I am wrong.