Skip to content

Instantly share code, notes, and snippets.

@shenjunru
Created September 30, 2012 15:23
Show Gist options
  • Save shenjunru/3807153 to your computer and use it in GitHub Desktop.
Save shenjunru/3807153 to your computer and use it in GitHub Desktop.
javascript: detect browser type by features detection
// IE
// conditional comment
(function(dom){
dom.innerHTML = '<!--[if IE]><br><![endif]-->';
return !!dom.firstChild.tagName;
})(document.createElement('p'));
// Conditional Compilation
// http://msdn.microsoft.com/en-us/library/ahx1z4fs(VS.80).aspx
IE=/*@cc_on!@*/!1;
IE55=/*@cc_on@if(@_jscript_version==5.5)!@end@*/!1;
IE6=/*@cc_on@if(@_jscript_version==5.6)!@end@*/!1;
IE7=/*@cc_on@if(@_jscript_version==5.7)!@end@*/!1;
IE8=/*@cc_on@if(@_jscript_version==5.8)!@end@*/!1;
// IE < 9
// Array bug
!-[1, ];
!','.split(/,/).length;
// no support of vertical tab character '\v'
!+'\v1'
!-'\v1'
'\v' == 'v'
// IE8
!!win.XDomainRequest
/********************************************************************************/
// FireFox
!!document.getBoxObjectFor || 'mozInnerScreenX' in window;
// FireFox 1.5
'some' in Array;
// FireFox 2.0
(function x(){})[-6]=='x'
// !!window.globalStorage;
// FireFox 3.0
(function x(){})[-5]=='x';
// !!window.MessageEvent;
// 'reduce' in Array
// FireFox 2.0 / 3.0
/a/[-1]=='a';
// FireFox 3.5
!!Object.getPrototypeOf;
// FireFox 3.6
'mozInnerScreenX' in window; // alt: !!document.body.mozMatchesSelector
// FireFox < 4 (Gecko < 2)
(function(o) { o[o] = o+""; return o[o] != o+""; })(new String("__count__"));
/********************************************************************************/
// Opera
!!window.opera && !!window.opera.toString().indexOf('Opera');
// Opera 9
/^function \(/.test([].sort);
/********************************************************************************/
// Safari
/a/.__proto__ == '//';
/********************************************************************************/
// chrome
/source/.test(/a/.toString+'');
/********************************************************************************/
// webkit
!!window.devicePixelRatio;
// http://www.thespanner.co.uk/2009/01/29/detecting-browsers-javascript-hacks/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment