-
-
Save masdeseiscaracteres/d92bb883f8e45c99f63a to your computer and use it in GitHub Desktop.
Detect if protocol handler is supported (serve through rawgit with proper Content-Type headers)
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
<!-- Accesible at: | |
http://rawgit.com/masdeseiscaracteres/d92bb883f8e45c99f63a/raw/2091770719285fbf33bab3e53c114fca7edd4719/BrowserDetectProtocol.html | |
--> | |
<html> | |
<head> | |
<title>Detect Custom 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">Launch</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; | |
} | |
//IE10+ | |
if(navigator.msLaunchUri){ | |
navigator.msLaunchUri(url, | |
function(){ isSupported = true; result(); }, //success | |
function(){ isSupported=false; result(); } //failure | |
); | |
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; | |
//Note: timeout could vary as per the browser version, have a higher value | |
setTimeout(function(){ | |
protcolEl.onblur = null; | |
result() | |
}, 500); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment