-
-
Save azproduction/1123026 to your computer and use it in GitHub Desktop.
Browser detection script v3.0
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
/* | |
({}).toString.call() | |
[object global] // chrome < 13 in 14 - [object Undefined] :( | |
[object Undefined] // ff | |
[object Window] // opera | |
[object DOMWindow] // safari | |
[object Object] // ie | |
*/ | |
function (a) { | |
// undefined magic | |
a = {}.toString.call()[8]; | |
return { | |
f: a == 'U', // is Firefox? | |
o: a == 'W', // is Opera? | |
s: a == 'D', // is Safari? | |
c: a == 'g', // is Chrome? | |
i: !a || a == 'O' // is IE? | |
} | |
} |
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
// 93 byte length | |
function(a){a={}.toString.call()[8];return{f:a=='U',o:a=='W',s:a=='D',c:a=='g',i:!a||a=='O'}} | |
/* alternative 77 byte length | |
function(){return{U:'f',W:'o',D:'s',g:'c',O:'i'}[{}.toString.call()[8]]||'i'} | |
*/ |
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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 azproduction | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
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
{ | |
"name": "browser", | |
"description": "Browser detection script", | |
"keywords": [ | |
"browser", | |
"detect", | |
"detection", | |
"firefox", | |
"opera", | |
"safari", | |
"chrome", | |
"ie" | |
] | |
} |
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
<pre id="br"></pre> | |
<script> | |
var b = function(a){a={}.toString.call()[8];return{f:a=='U',o:a=='W',s:a=='D',c:a=='g',i:!a||a=='O'}}(); | |
document.getElementById('br').innerHTML = b.f && 'Firefox' || | |
b.o && 'Opera' || | |
b.s && 'Safari' || | |
b.c && 'Chrome' || | |
b.i && 'Ie' || | |
'???' ; | |
</script> | |
<!-- Alt version --> | |
<pre id="br2"></pre> | |
<script> | |
var b = function(){return{U:'f',W:'o',D:'s',g:'c',O:'i'}[{}.toString.call()[8]]||'i'}(); | |
document.getElementById('br2').innerHTML = {f:'Firefox',o:'Opera',s:'Safari',c:'Chrome',i:'IE'}[b]; | |
</script> |
I can just pass undefined to call()
and ill get the same. ({}).toString.call()
same as ({}).toString.call(void 0)
. this.prototype
is undefined aswell... :)
@tsaniel sad that it was fixed
One more thing, IE7 doesn't support array-like string.
yep i use fallback !a||a=='O'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An awesome detection!
But my Chrome 14 shows [object Undefined] as Firefox.