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
  • 3
    What is this nonsense?? Commented Jul 16, 2013 at 17:16
  • if you are using jquery why not use it to detect browser version and do a conditional execution. Commented Jul 16, 2013 at 17:21
  • possible duplicate of Conditional comment for 'Except IE8'? Commented Jul 16, 2013 at 17:24
  • I marked the duplicate, same idea, instead of 8, use 7. Commented Jul 16, 2013 at 17:25

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

try this

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

2 Comments

note that $.browser got removed from jQuery with version 1.9.
This question isn't tagged with jQuery (although it is used in code)
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]>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.