views:

77

answers:

7

I want to include a javascript file only if the browser is not IE; is there any way to do this?

+1  A: 

You can do that totally different way [inserting something ONLY if IE] using Conditional Comments, maybe that would help you.

http://en.wikipedia.org/wiki/Conditional_comment

Tomasz Kowalczyk
You can also insert something only when it's *not* IE, using IE's conditional comments. Yes, really.
T.J. Crowder
Good to know - thanks!
Tomasz Kowalczyk
+2  A: 

You can use an IE conditional comment.

To get something that will show up in browsers other than IE, but not in IE, and which will still validate, you can use:

<!--[if !IE]>-->
  <script src="..."></script>
<!--<![endif]-->
Brian Campbell
A: 

How about turning it around? If this is a possibility you can use IE's conditional comments: http://www.quirksmode.org/css/condcom.html

Otherwise you could sniff user-agents but this is considered 'bad practice'.

ikanobori
You don't have to turn it around, IE's conditional comments can be used for targeting non-IE browsers (yes, really).
T.J. Crowder
+2  A: 

You can do it with IE's conditional comments, like so:

<![if !IE]>
<script src="your-non-IE-script.js" type="text/javascript"></script>
<![endif]>

Note that the above is processed by non-IE browsers because the conditional is not an HTML comment, but a processing instruction, so the bit in the middle is processed by non-IE browsers. IE sees the conditional and skips over the content because it understands the conditional means "Not you, move along."

If you want to do something only for IE, you use a form that's similar, but uses HTML comments instead (with the --) because that's the only way you can rely on other browsers ignoring the contents. IE knows to pay attention to them, even though they're comments. More on the link above.

Note that there's a page load speed implication on IE (not the other browsers) when you use conditional comments (they temporarily block download of other resources), more here: http://www.phpied.com/conditional-comments-block-downloads/

T.J. Crowder
I would just like to note that that is not a processing instruction. It's just proprietary Microsoft syntax. A processing instruction is in the form of `<?processing-instruction-name-here ...data here...?>`.
Eli Grey
@Eli: Sure enough (http://en.wikipedia.org/wiki/Processing_Instruction). Wow, I always thought they were processing instructions. Thanks!
T.J. Crowder
+2  A: 

First, a note: This isn't really a good practice. If possible, you should strive to design your website in a browser-agnostic way, so that it works consistently across all browsers without the need to maintain hacks and tricks for browser-specific problems.

But if you really want to do this, it's easiest to include a file only if the browser is IE:

<!--[if lt IE 7]>
<script type="text/javascript" src="global.js"></script>
<![endif]-->

(Includes the file only if the browser is IE6 or less.)

However, if you really want to do it the other way around, here are some of your options:

  1. Use server-side browser sniffing to process the browser before the page is drawn. That is, use a server-side language (Java, PHP, whatever) to determine what the browser is (usually through the user agent string) and then conditionally include your JS files that way. (For example, you can use PHP's get_browser function.)
  2. Use client-side browser sniffing to call another JS file if the browser is not IE. You can determine the browser using JavaScript itself, and then insert another JS file into the page if the browser is anything but IE. (For example, you can use jQuery's browser function.)
  3. T.J.'s answer provides a way of doing it with I.E.'s conditional comments as well.
Josh Leitzel
+1  A: 

Yes, conditional scripts for IE is your answer:

<!--[if lt IE 7 ]>
  <script src="/js/my_ie_script.js"></script>
<![endif]-->
mkoistinen
That will do the quasi-opposite of what the OP asked for.
T.J. Crowder
Dang, you're correct. How did I miss the 'not' in the OP's question. Its late!
mkoistinen
+1  A: 

It's lesser known than conditional comments, but I thought I'd mention that you can also use an IE feature called conditional compilation - see http://msdn.microsoft.com/en-us/library/7kx09ct1%28VS.80%29.aspx

This example comes from their docs:

/*@cc_on @*/
/*@if (@_jscript_version >= 5)
document.write("JScript Version 5.0 or better.<BR>");
@else @*/
document.write("You need a more recent script engine.<BR>");
/*@end @*/
Mark Snidovich