I want to include a JavaScript file only if the browser is not IE. Is there any way to do this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Update 2022:
Some options for you:
Have your server look at the
User-Agentheader and send different HTML to Internet Explorer vs. other browsers.Pros:
Cons:
User-Agentstrings is notoriously error-prone, and they can be spoofed or entirely absent.Sniff
navigator.userAgentand either output ascripttag or not depending on what you find.Pros:
Cons:
User-Agentstrings is notoriously error-prone, and they can be spoofed or entirely absent.scripttags from code, particularly viadocument.write, but depending on your use casedocument.writemay be unavoidable (e.g., do you need the script there before some other script, etc.).Sniff for something in the JavaScript runtime that only Internet Explorer has, and output a
scripttag or not based on what you find.Pros:
Cons:
I’d probably look at #3. For instance, any even vaguely modern browser has the
Symbolfunction. Internet Explorer does not. So:Symbolis just one example, IE lacksReflect,Proxy, and a few others from ES2015 that everything else has now…(Not sure why I didn’t mention this in 2013!)
Update 2013: IE10+ don’t support conditional comments anymore.
You can do it with IE’s conditional comments, like so:
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/