Created
September 13, 2012 19:26
-
-
Save rajeshsegu/3716941 to your computer and use it in GitHub Desktop.
Detect Browser Custom Protocols
This file contains 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">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> |
IE10/11 on Windows 7 doesn't support msLaunchUri(), only on Windows 8
http://stackoverflow.com/questions/23396303/mslaunchuri-not-available-in-ie11
Chrome 40.0.2214.45 beta-m (64-bit) works.
Is there any way to make it work in Safari?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should this work locally? I am having an issue with IE10+