Awesome New Way to Detect Internet Explorer with JavaScript


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  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

You need to login to post a comment.