Reputation: 625
How would I go about running a script in every browser except for Internet Explorer?
I'm using queryloader2 on my site, and (for whatever reason) it takes around a minute to load in IE9, I'm assuming the same or worse in older versions of IE. In any other browser, it's just a few seconds. Due to this, I'd like to just not have the script run at all in any version of IE (it doesn't truly hurt the functionality of the site).
Upvotes: 5
Views: 10405
Reputation: 29519
The conditional comments in all the other answers (at the time of this posting) are incorrect, and will be interpreted as either (invalid) HTML or completely ignored as an HTML comment in non-IE browsers.
It is important to understand that as far as any browser other than IE is concerned (and even newer versions of IE), the "conditional HTML comment" is just a comment. In other words, something like the upvoted answer's suggestion:
<!--[if !IE]>
<script src="myScript.js" type="text/javascript"></script>
<![endif]-->
Is, as far as anything other than IE is concerned, just one long comment, starting at the <!--
on line 1 and ending at the -->
on line 3, just like any other HTML comment.
To get a chunk of HTML to be ignored by IE, you have to take advantage of the fact that HTML comments do not support nesting, i.e. <!-- <!-- --> something -->
is a comment that ends at the first -->
and not the second, meaning something -->
will be parsed as HTML and not considered part of the comment.
So what we can do is use something like this instead:
<!--[if !IE]-->
<script type="text/javascript" src="script.js"></script>
<!--<![endif]-->
Here, non-IE browsers will see two separate comments (on the 1st and 3rd lines, respectively) and regular HTML in the middle. But a browser parsing these tags will see a comment starting on the first line, then ignore everything until encountering <![endif]-->
on the 3rd line. This gives us a block of code that will be executed by non-IE/modern browsers, but will be ignored by old versions of IE supporting conditional HTML.
Upvotes: 5
Reputation: 129
below code is may be fine but unfortunately not working in my problem,,,,,,,,,,,,,,,so if above code is not working , then use this
<![if !IE]>You are NOT using IE.<![endif]
Upvotes: -2
Reputation: 27765
You need to use conditional comments for that:
<!--[if !IE]>
<script src="myScript.js" type="text/javascript"></script>
<![endif]-->
Upvotes: 6