最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Execute certain javascript if browser lt ie9 - Stack Overflow

programmeradmin1浏览0评论

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

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

But what i want is to check this condition within the scipt tag and with jQuery document.ready

<script>
    $(document).ready(function(){
        //code to check if lt ie9 
    });
</script>

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

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

But what i want is to check this condition within the scipt tag and with jQuery document.ready

<script>
    $(document).ready(function(){
        //code to check if lt ie9 
    });
</script>
Share Improve this question edited Aug 9, 2013 at 11:48 André Dion 21.7k7 gold badges58 silver badges60 bronze badges asked Aug 9, 2013 at 11:37 melvindiditmelvindidit 4402 gold badges6 silver badges16 bronze badges 2
  • possible duplicate of Detect IE version in Javascript – JJJ Commented Aug 9, 2013 at 11:39
  • Have you considered using feature detection rather than browser detection? – Spudley Commented Aug 9, 2013 at 11:54
Add a comment  | 

2 Answers 2

Reset to default 14

You can target older Internet Explorer versions (<= 9) through classes on 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 <html> has the class .lt9 or .lt8 — whatever version of Internet Explorer you are targeting:

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

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

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

发布评论

评论列表(0)

  1. 暂无评论