Created
September 13, 2012 19:26
-
-
Save rajeshsegu/3716940 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="/public/unittest/jasmine/jquery.js"></script> | |
</head> | |
<body> | |
<input id="protocol" value="" placeholder="custom protocol"/> | |
<button id="launch">Launch</button> | |
<!-- Mozilla Only --> | |
<iframe id="hiddenIframe" src="about:blank"></iframe> | |
<!-- IE Case 1 --> | |
<a id="hiddenLink" style="display:none;" href="#">custom protocol</a> | |
<script> | |
var isSupported = false; | |
function getProtocol(){ | |
return $('#protocol').val(); | |
} | |
function getUrl(){ | |
return getProtocol()+"://"+"rajeshsegu.com"; | |
} | |
function result(){ | |
alert(getProtocol() + " supported => " + isSupported); | |
} | |
$('#launch').click(function(){ | |
if($.browser.mozilla){ | |
launchMozilla(); | |
}else if($.browser.chrome){ | |
launchChrome(); | |
}else if($.browser.msie){ | |
launchIE(); | |
} | |
}); | |
function launchIE(){ | |
var url = getUrl(), | |
aLink = $('#hiddenLink')[0]; | |
isSupported = false; | |
aLink.href = url; | |
//Case 1: protcolLong | |
if(navigator.appName=="Microsoft Internet Explorer" | |
&& aLink.protocolLong=="Unknown Protocol"){ | |
isSupported = false; | |
result(); | |
return; | |
} | |
//Case2: Open New Window | |
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){ | |
console.log(e); | |
} | |
if(isSupported){ | |
myWindow.setTimeout('window.close()', 100); | |
}else{ | |
myWindow.close(); | |
} | |
result(); | |
}, 100) | |
}; | |
function launchMozilla(){ | |
var url = getUrl(), | |
iFrame = $('#hiddenIframe')[0]; | |
isSupported = false; | |
try{ | |
iFrame.contentWindow.location.href = url; | |
isSupported = true; | |
result(); | |
}catch(e){ | |
//FireFox | |
if (e.name == "NS_ERROR_UNKNOWN_PROTOCOL"){ | |
isSupported = false; | |
result(); | |
} | |
} | |
} | |
function launchChrome(){ | |
var url = getUrl(), | |
protcolEl = $('#protocol')[0]; | |
isSupported = false; | |
protcolEl.focus(); | |
protcolEl.onblur = function(){ | |
isSupported = true; | |
}; | |
location.href = url; | |
setTimeout(function(){ | |
protcolEl.onblur = null; | |
result() | |
}, 300); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
IE9 launchIE can not work. launchChrome and Firefox work well