Run specific javascript if lt ie9 browser

I want to execute specific code in jQuery if the browser is lower than IE9. And yes, I already know about

<!--[if ... IE ]> <![endif]--> 

But I want to check this condition in the scipt tag and with jQuery document.ready

 <script> $(document).ready(function(){ //code to check if lt ie9 }); </script> 
+6
source share
2 answers

You can target older versions of Internet Explorer (<= 9) to classes in the <html> element ...

 <!--[if lt IE 7]> <html class="ie ie6 lte9 lte8 lte7"> <![endif]--> <!--[if IE 7]> <html class="ie ie7 lte9 lte8 lte7"> <![endif]--> <!--[if IE 8]> <html class="ie ie8 lte9 lte8"> <![endif]--> <!--[if IE 9]> <html class="ie ie9 lte9"> <![endif]--> <!--[if gt IE 9]> <html> <![endif]--> <!--[if !IE]><!--> <html> <!--<![endif]--> 

... and then check if the <html> class is .lt9 or .lt8 - any version of Internet Explorer that you are targeting:

 if($('html').hasClass('lte9')) { /* LTE IE9 */ } 

However, I would suggest using Modernizr feature detection instead of checking for a specific browser.

+14
source

Use feature detection instead of browser detection. i.e. http://modernizr.com/

+2
source

Source: https://habr.com/ru/post/951392/


All Articles