1

I got a scenario where I need a JavaScript to load in all browsers except IE7. Any help is greatly appreciated. I've tried the below code:

<!--[if gt IE 7]>
<script type="text/javascript">
    //Some script XXX to execute
</script>
<![endif]-->
<!--[if (gt IE 9)|!(IE)]><!-->
<script type="text/javascript">    
    //Some script XXX to execute
</script>
<!--<![endif]-->

4 Answers4

1

I think this works too

<!--[if !(IE 7)]>
   // tag here
<![endif]-->

You can also do a combination of

<![if !IE]>
   // script tag
<![endif]>

<!--[if (gte IE 8)&(lt IE 7)]>
   // script tag
<![endif]-->

see docs here http://msdn.microsoft.com/en-us/library/ms537512%28v=vs.85%29.aspx

I am not sure if you are looking to load an entire file or just a few lines of script. If it's only a few lines the jquery way is easier

Huangism
  • 16,278
  • 7
  • 48
  • 74
0

Use navigator.userAgent to check the browser identification. also see here

Community
  • 1
  • 1
brandon
  • 555
  • 1
  • 6
  • 15
0

try this

if ( $.browser.msie == true &&  $.browser.version < 8) {
           //code for ie 7
        }
else
{
       document.write("<script src=\"test.js\">");
}
user1
  • 1,063
  • 1
  • 8
  • 28
0

Looks like you're already on the right lines with conditional comments

<!--[if !(IE 7)]>--><script>alert("I'm not IE7");</script><!--<![endif]-->

To any non-IE browser, this becomes

<!--[if !(IE 7)]>-->                     Comment
<script>alert("I'm not IE7");</script>   Element
<!--<![endif]-->                         Comment

To any IE browser, this becomes

<!--[if !(IE 7)]>                        If not IE 7
-->                                      (continued) AND Comment
<script>alert("I'm not IE7");</script>   Element
<!--<![endif]-->                         Comment AND End if

From which, if it is IE 7, it gets snipped.

<!--[if !(IE 7)]>                        If - not rendered
                                         SNIP
<![endif]-->                             End If

You could reduce this down if you don't mind invalid HTML

<![if !(IE 7)]><script>alert("I'm not IE7");</script><![endif]>
Paul S.
  • 64,864
  • 9
  • 122
  • 138