user2587958
user2587958

Reputation: 11

disable a javascript only in IE7

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]-->

Upvotes: 1

Views: 663

Answers (4)

Paul S.
Paul S.

Reputation: 66324

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]>

Upvotes: 0

Huangism
Huangism

Reputation: 16438

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

Upvotes: 1

user1
user1

Reputation: 1065

try this

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

Upvotes: 0

brandon
brandon

Reputation: 595

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

Upvotes: 0

Related Questions