I want to include a javascript file only if the browser is not IE; is there any way to do this?
You can do that totally different way [inserting something ONLY if IE] using Conditional Comments, maybe that would help you.
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]-->
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'.
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/
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:
- 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.) - 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.) - T.J.'s answer provides a way of doing it with I.E.'s conditional comments as well.
Yes, conditional scripts for IE is your answer:
<!--[if lt IE 7 ]>
<script src="/js/my_ie_script.js"></script>
<![endif]-->
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 @*/