Detecting browsers javascript hacks

Back to articles

hackvertor

Author:

Gareth Heyes

@hackvertor

Published: Thu, 29 Jan 2009 09:26:59 GMT

Updated: Sat, 22 Mar 2025 15:38:12 GMT

I enjoyed my last experiment to create tiny browser detection hacks, so I thought I'd try and do some more in other browsers. I've found these while testing Hackvertor and writing the inspection functions. The rules are simple if you want to post your own:-

  1. The variable assignment must be the abbreviation of the browser. E.g. FF, IE, Op, Saf, Chr.
  2. The detection shouldn't be able to be overwritten. E.g. IE=!!top.execScript is incorrect because a web site can redefine execScript as a variable or function.
  3. Small and as fast as possible please.

To start off, this one was found by DoctorDan from the slackers forums:-

<pre lang="javascript"> //Firefox detector 2/3 by DoctorDan FF=/a/[-1]=='a' </pre> <pre lang="javascript"> //Firefox 3 by me:- FF3=(function x(){})[-5]=='x' </pre> <pre lang="javascript"> //Firefox 2 by me:- FF2=(function x(){})[-6]=='x' </pre> <pre lang="javascript"> //IE detector I posted previously IE='\v'=='v' </pre> <pre lang="javascript"> //Safari detector by me Saf=/a/.__proto__=='//' </pre> <pre lang="javascript"> //Chrome by me Chr=/source/.test((/a/.toString+'')) </pre> <pre lang="javascript"> //Opera by me Op=/^function \(/.test([].sort) </pre> <pre lang="javascript"> //IE6 detector using conditionals try {IE6=@cc_on @_jscript_version <= 5.7&&@_jscript_build<10000} catch(e){IE6=false;} </pre>

As I come across more developing Hackvertor I shall post them here. Please post your own or verify any of the ones posted.

Update...

One line to rule them all:-

<pre lang="javascript"> 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' </pre>

Back to articles