Tiny Javascript to detect versions of Internet Explorer

Written November 23. 2006, at 00:06 GMT.

Here's a nifty little piece of Javascript code that reliably detects the presence of Microsoft's Internet Explorer browser with absolutely minimal processing overhead for all other browsers:

var is_ie/*@cc_on = {
  // quirksmode : (document.compatMode=="BackCompat"),
  version : parseFloat(navigator.appVersion.match(/MSIE (.+?);/)[1])
}@*/;

(Uncomment the second line if you also wish to check whether IE is running in "compatibility" (quirks) mode or in standards-mode.)

The check relies on Microsoft's proprietary conditional compilation syntax, to make sure the object assignment and version number parsing is only performed by Microsoft's browsers.

All other browsers cheerfully ignore the Javascript comment block (/* ... */) so what they end up with is essentially this:

var is_ie;

...which evaluates as an implicit false, when used as a condition. This allows us to set up conditions like this one:

if (is_ie && (is_ie.version < 7))
{
  // do IE specific stuff
}
else
{
  // default behavior for other browsers
}

I lifted the basic idea from Javascript God, Dean Edwards, and extended it to include the version number - which is tremendously useful when dealing with IE's quirky behavior.


More like this: English Entries, HTML/CSS, Javascript.


Reader Comments (6)

  1. Sindri replies:

    if ( is_ie ) { document.location.href = "http://www.mozilla.com/en-US/firefox/"; }

    :oŢ

    November 23. 2006 kl. 12:19 GMT | #

  2. Kiddi Geir replies:

    hehe góđur : )

    November 24. 2006 kl. 12:40 GMT | #

  3. Már Örlygsson: Minor Javascript updates

    "Tonight I updated the code provided in a couple of recent posts: Added an optional feature to the tiny IE browser detection script Posted a significant update to my unobtrusive script workaround for IE's "Click to activate" feature, that makes..." Read more

    December 13. 2006 kl. 23:46 GMT | #

  4. Tsuruda replies:

    hi there cheap williams arizona hotels http://travel-hotel-resources.info/cheap-hotels/cheap-amsterdam-hotels.php bangkok thailand hotels flights dirt cheap airfares cheap hotels in zurich, switzerland cheap hotels in west london http://travel-hotel-resources.info/cheap-hotels/cheap-hotels-or-hostels-in-amsterdam.php cheap amsterdam hotels http://travel-hotel-resources.info/cheap-hotels/cheap--hotels.php cheap hotels in anaheim disneyland cheap hotels in burlington vermont http://travel-hotel-resources.info/cheap-hotels/best-cheap-hotels-in-san-francisco.php cheap airfare travel deals discount hotels sidestep travel http://travel-hotel-resources.info/cheap-hotels/pigeon-forge-cheap-hotels.php

    November 13. 2007 kl. 04:29 GMT | #

  5. Walt replies:

    hi there cheap flights and hotels for las vegas nevada cheap flights hotels in las vegas nv http://travel-hotel-resources.info/cheap-hotels/cheap-hotels-in-helsinki.php http://travel-hotel-resources.info/cheap-hotels/cheap-oceanfront-hotels-in-ocean-city-maryland.php http://travel-hotel-resources.info/cheap-hotels/cheap-central-london-hotels.php cheap hotels louisville http://travel-hotel-resources.info/cheap-hotels/cheap-hotels-in-kuala-lumpur.php cheap pensions, b&b, hotels ina around garmish, germany http://travel-hotel-resources.info/cheap-hotels/cheap-hotels-in-blackpool.php cheap flights and france hotels http://travel-hotel-resources.info/cheap-hotels/cheap-hotels-in-dubai-during-november.php http://travel-hotel-resources.info/cheap-hotels/cheap-hotels-in-solvang.php http://travel-hotel-resources.info/cheap-hotels/find-cheap-hotels.php

    November 13. 2007 kl. 04:29 GMT | #

  6. Van Prooyen replies:

    hi there http://travel-hotel-resources.info/cheap-hotels/luxury-las-vegas-hotels-lowest-rates-cheap-hotel-discount.php http://travel-hotel-resources.info/cheap-hotels/cheap-hotels-in-madrid.php cheap deals for london hotels http://travel-hotel-resources.info/cheap-hotels/cheap-hotels-newnan,-ga.php http://travel-hotel-resources.info/cheap-hotels/cheap-parking-hotels-gatwick.php cheap hotels in tarrytown, ny http://travel-hotel-resources.info/cheap-hotels/cheap-hotels-brno.php cheap hotels in baltimore, md cheap hotels in downtown atlanta ga http://travel-hotel-resources.info/cheap-hotels/cheap-hotels-in-greece.php cheap hotels in bellevue cheap hotels in seaside oregon cheap hotels basel

    November 13. 2007 kl. 04:29 GMT | #

Ţessum svarhala hefur veriđ lokađ. Kćrar ţakkir til ţeirra sem tóku ţátt í umrćđunni.


 

Flakk um vefsvćđiđ



 

Nýleg svör frá lesendum

  • Rich (Req.js - javascript lazy-loading and dependency managment made easy)
  • Rich (Req.js - javascript lazy-loading and dependency managment made easy)
  • Rich (Req.js - javascript lazy-loading and dependency managment made easy)
  • Már (Req.js - javascript lazy-loading and dependency managment made easy)
  • Rich (Req.js - javascript lazy-loading and dependency managment made easy)
  • Már (Req.js - javascript lazy-loading and dependency managment made easy)
  • Dinesh (Req.js - javascript lazy-loading and dependency managment made easy)
  • Már (Taubleyjur í nútímanum - lítill leiđarvísir handa hrćddri ţjóđ)
  • Ada (Taubleyjur í nútímanum - lítill leiđarvísir handa hrćddri ţjóđ)
  • notandi (Taubleyjur í nútímanum - lítill leiđarvísir handa hrćddri ţjóđ)
  • Geir (Lausnin á efnahagsvandanum)
  • Jenný (Lausnin á efnahagsvandanum)
  • Óli Jens (Lausnin á efnahagsvandanum)
  • Már (Lausnin á efnahagsvandanum)
  • Kjartan S (Lausnin á efnahagsvandanum)

 

 

Yfirlit yfir ţetta skjal

(Atriđin í listanum vísa á ákveđna kafla ofar á síđunni.)