Detecting browsers javascript hacks in a tweet


Hi folks.

I am recently very fond of the term “code in a tweet” which means that a very useful piece of source code can even fit a Twitter post.  Below here is the shortest javascript that can detect IE browser by Andrea (yeah, IE sucks and we have to detect it to eliminate its problems)


IE='\v'=='v'

Moreover, Gareth Heyes has come up with other browser detections and gather them together


//Firefox detector 2/3 by DoctorDan
FF=/a/[-1]=='a'
//Firefox 3
FF3=(function x(){})[-5]=='x'
//Firefox 2
FF2=(function x(){})[-6]=='x'
//IE detector I posted previously
IE='\v'=='v'
//Safari detector
Saf=/a/.__proto__=='//'
//Chrome
Chr=/source/.test((/a/.toString+''))
//Opera
Op=/^function \(/.test([].sort)

Usage: The variable assignment is the abbreviation of the browser e.g. FF, IE, Op, Saf, Chr which return true or false.

And this is the most interesting part: one-liner to rule them all

B=(function x(){})[-5]=='x'?'FF3':(function x(){})[-6]=='x'?'FF2':/a/[-1]=='a'?'FF':'\v'=='v'?'IE':/a/.__proto__=='//'?'Saf':/s/.test(/a/.toString)?'Chr':/^function \(/.test([].sort)?'Op':'Unknown'

Usage: B will return a string of the abbreviation of the browser
Tell me what do you think? And if you find any cool stuff like that let me know.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s