Skip to content

Instantly share code, notes, and snippets.

@azproduction
Forked from 140bytes/LICENSE.txt
Created May 24, 2011 19:16
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save azproduction/989440 to your computer and use it in GitHub Desktop.
Desktop browser detection script
/**
* Modern desktop browser detector
*
* @returns {Object}
*/
function (a) {
// Worker.prototype.toString() magic see http://goo.gl/tD1jr
a = ((this.Worker || 0).prototype + '').length;
return {
f: a == 36, // is Firefox?
o: a == 33, // is Opera?
s: a == 24, // is Safari?
c: a == 15, // is Chrome?
i: a == 9 // is IE?
}
}
// Alternative version
function () {
return '?icsof' [
// same magic
((this.Worker||0).prototype+'').length / 7 | 0
// fallback for ie6 :3
] || 'i'
}
function(a){a=((this.Worker||0).prototype+'').length;return{f:a==36,o:a==33,s:a==24,c:a==15,i:a==9}}
/* Alternative 75 byte version
function(){return'?icsof'[((this.Worker||0).prototype+'').length/7|0]||'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": "Desktop browser detection script",
"keywords": [
"browser",
"detect",
"detection",
"firefox",
"opera",
"safari",
"chrome",
"ie"
]
}
<pre id="br"></pre>
<script>
var b = function(a){a=((this.Worker||0).prototype+'').length;return{f:a==36,o:a==33,s:a==24,c:a==15,i:a==9}}();
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'?icsof'[((this.Worker||0).prototype+'').length/7|0]||'i'}();
document.getElementById('br2').innerHTML = {f:'Firefox',o:'Opera',s:'Safari',c:'Chrome',i:'IE'}[b];
</script>
@atesgoral
Copy link

Delightfully hacky!

@atk
Copy link

atk commented May 30, 2011

Really nice Idea. You can shave 3 chars off that, though:

function(a){a=((this.Worker||0).prototype+'').length;return {f:a==36,o:a==33,s:a==24,c:a==15,i:a==9}}

Regards, LX

@azproduction
Copy link
Author

@atk thx!

@atk
Copy link

atk commented Jul 22, 2011

Hi, there. I just found that all of these numbers could be divided by 3, which rather slims down a string with the corresponding identifiers:

function(a){a=((this.Worker||0).prototype+'').length;return " i c s o f"[a/3]}

will do almost the same while golfing the script down to 82 bytes.

or if we divide by 5 and flooring the result:

function(a){a=((this.Worker||0).prototype+'').length;return 'i cs of'[a/5|0]}

still same result, yet only 78 bytes.

Update: will not run on older IEs, because they cant do String[number]...

@azproduction
Copy link
Author

Hi, nice idea! If we divide by 7 and use ||'i' for older ie and discard a we will get 76 bytes code:
function(){return '?icsof'[((this.Worker||0).prototype+'').length/7|0]||'i'}

@dciccale
Copy link

dciccale commented Aug 1, 2011

you can save a byte removing the space between return and {f:a...

@atk
Copy link

atk commented Aug 1, 2011

or return'?icsof'[...] in your alternate version

@azproduction
Copy link
Author

Thx!
I've just created another browser detection script using same magic on undefined ({}).toString.call(). Now it fully cross-browser(desktop and mobile). It free from Worker, but a bit longer.

https://gist.github.com/1123026

@atk
Copy link

atk commented Aug 3, 2011

That's really cool. Since I'm working on mobile stuff, too, this will definitely be useful. Could we have support/detection for Symbian, Blackberry, Netfront Access, Obigo and some of the other mobile browsers?

@azproduction
Copy link
Author

I own only iOS and Android, so i cant test on others. This is my test http://jsfiddle.net/azproduction/V4LeE/ i used to fill this table http://goo.gl/tD1jr you may use it.
This script is small and hacky but its not a good idea to identify a browser using a hack :)

@atk
Copy link

atk commented Aug 4, 2011

usually I filter the essential portion out of the userAgent.

@azproduction
Copy link
Author

a good way, except for a fake User-Agent

@atk
Copy link

atk commented Aug 4, 2011

I know - but it makes testing easier as an added value ;-)

@jdalton
Copy link

jdalton commented Aug 23, 2011

@azproduction I know the gist description says "Desktop browser detection", but I thought I would mention that this fails on my iPhone 3gs (3.1.3) Safari 4 because it seems to lack Worker support. Also IE 10 reports [object WorkerPrototype] and so will be wrongly detected as Safari.

@webschik
Copy link

Hi!
In Firefox 26.0 (Ubuntu 13.04) return "isChrome'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment