Fćrslur fimmtudaginn 23. nóvember 2006

At 00:06: Tiny Javascript to detect versions of Internet Explorer 

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.

Reader comments (6) | Permalink


 

Flakk um vefsvćđiđ



 

Fćrslur í nóvember 2006

nóvember 2006
SunMán ŢriMiđ FimFös Lau
      1. 2. 3. 4.
5. 6. 7. 8. 9. 10. 11.
12. 13. 14. 15. 16. 17. 18.
19. 20. 21. 22. 23. 24. 25.
26. 27. 28. 29. 30.    

Nýleg svör frá lesendum

  • ma801zda (Vélrćnar ţýđingar)
  • tapasbocnoa (HOWTO: Fix the hCalendar and hCard accessibility and i18n problems)
  • Siggi Palli (Vélrćnar ţýđingar)
  • Bjarni Rúnar (HOWTO: Fix the hCalendar and hCard accessibility and i18n problems)
  • Unnur María (Vélrćnar ţýđingar)
  • Már (Vélrćnar ţýđingar)
  • Valdís (Vélrćnar ţýđingar)
  • Már (Vélrćnar ţýđingar)
  • Hrafnkell (Vélrćnar ţýđingar)
  • Már (Vélrćnar ţýđingar)
  • Óli Gneisti (Vélrćnar ţýđingar)
  • Sindri (Vélrćnar ţýđingar)
  • Just To Say (The Elegant, Unobtrusive Javascript Workaround for "Click to activate and use this control")
  • Finnur (hCalendar og hCard ófýsilegir stađlar fyrir íslenska vefi?)

 

 

Yfirlit yfir ţetta skjal

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