Posted By

Nettuts on 08/17/10


Tagged


Versions (?)

Who likes this?

10 people have marked this snippet as a favorite

gdespeaux
digitalbart
seanpowell
barbietunnie
ahmedalttai
kwameboame
qubestream
leobetosouza
guilhermehn
Arvi


Awesome New Way to Detect Internet Explorer with JavaScript


 / Published in: JavaScript
 

  1. // Credit goes to James Padolsey - http://james.padolsey.com
  2. // http://twitter.com/padolsey/status/21343465477
  3.  
  4. // ----------------------------------------------------------
  5. // If you're not in IE (or IE version is less than 5) then:
  6. // ie === undefined
  7. // If you're in IE (>5) then you can determine which version:
  8. // ie === 7; // IE7
  9. // Thus, to detect IE:
  10. // if (ie) {}
  11. // And to detect the version:
  12. // ie === 6 // IE6
  13. // ie > 7 // IE8, IE9 ...
  14. // ie < 9 // Anything less than IE9
  15. // ----------------------------------------------------------
  16.  
  17. var ie = (function(){
  18.  
  19. var undef, v = 3, div = document.createElement('div');
  20.  
  21. while (
  22. div.innerHTML = '<!--[if gt IE '+(++v)+']><i></i><![endif]-->',
  23. div.getElementsByTagName('i')[0]
  24. );
  25.  
  26. return v > 4 ? v : undef;
  27.  
  28. }());

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: leobalter on November 22, 2011

to make it faster (but less readable) the line 26 could be something like:

Javascript return (v > 4 && v) || undef;

Posted By: leobalter on November 22, 2011

to make it faster (but less readable) the line 26 could be something like:

Javascript return (v > 4 && v) || undef;

Posted By: leobalter on November 22, 2011

ops, back button and markdown aren't work great, sorry.

You need to login to post a comment.