Skip to content

Instantly share code, notes, and snippets.

@azproduction
Forked from 140bytes/LICENSE.txt
Created August 3, 2011 16:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save azproduction/1123026 to your computer and use it in GitHub Desktop.
Save azproduction/1123026 to your computer and use it in GitHub Desktop.
Browser detection script v3.0
/*
({}).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?
}
}
// 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'}
*/
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.
{
"name": "browser",
"description": "Browser detection script",
"keywords": [
"browser",
"detect",
"detection",
"firefox",
"opera",
"safari",
"chrome",
"ie"
]
}
<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>
@tsaniel
Copy link

tsaniel commented Aug 4, 2011

An awesome detection!
But my Chrome 14 shows [object Undefined] as Firefox.

@azproduction
Copy link
Author

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

@tsaniel
Copy link

tsaniel commented Aug 4, 2011

One more thing, IE7 doesn't support array-like string.

@azproduction
Copy link
Author

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