28

I'm using an image carousel script that is quite heavy on the browser. It works great in Opera and Chrome, half decent in FF and absolutely breaks my balls in IE. So i'd like to give IE users an alternative of simple HTML without any action/JS.

The script doesn't use MT or jQuery and its like 380 lines of JS. Would it be possible to give IE users a plain HTML alternative?

var browserName=navigator.appName; if (browserName=="Microsoft Internet Explorer") { // what command can i use? }

Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
dimmy4
  • 307
  • 1
  • 3
  • 5
  • 1
    Mind that all (or almost) the answers below mention conditional comments. Conditional comments have been deprecated since IE10. See http://blogs.msdn.com/b/ie/archive/2011/07/06/html5-parsing-in-ie10.aspx?Redirected=true – Adriano Aug 23 '13 at 13:22
  • Other people come to the questions posted here for answers, sometimes many years after the question was initially asked Victor. @AdrienBe is politely letting others know that the answers with conditional comments are no longer viable. Downvotes on deprecated answers are helpful to people coming to the thread to learn what to do for this problem. Votes let others know what the community thinks is the best answer, and what answers should not be tried. Try to not appear so wounded by the process. – Stephen M Irving Jan 10 '20 at 17:12

10 Answers10

21

This article is quite explanatory: http://msdn.microsoft.com/en-us/library/ms537509%28v=vs.85%29.aspx.

If your JS is unobtrusive, you can just use:

<![if !IE]>
   <script src...
<![endif]>
Flack
  • 5,862
  • 2
  • 23
  • 27
20

You can do something like this to include IE-specific javascript:

<!--[IF IE]>
    <script type="text/javascript">
        // IE stuff
    </script>
<![endif]-->
Seth
  • 45,033
  • 10
  • 85
  • 120
  • 1
    you forgot the "!", I think: – DPM Apr 14 '13 at 14:52
  • Just noting that this only works on IE9 and below. Since the only version of IE that is in any use at all in 2020 is IE 11 this method is no longer viable. – Stephen M Irving Jan 06 '20 at 15:29
  • You are correct - see: https://stackoverflow.com/a/22187600/65295. Hopefully nobody is still developing specifically for IE :) – Seth Jan 06 '20 at 20:31
15

For IE10+ standard conditions don't work cause of engine change or some another reasons, cause, you know, it's MSIE. But for IE10+ you need to run something like this in your scripts:

if (navigator.userAgent.match(/Trident\/7\./)) {
  // do stuff for IE.
}
Stephen M Irving
  • 1,324
  • 9
  • 20
Y.Puzyrenko
  • 2,166
  • 1
  • 15
  • 23
  • Most useful up to date answer. Should be at the top. – carla Jul 15 '15 at 17:58
  • You don't actually need the double bang `!!` at the start for it to automatically convert itself into a Boolean. – Stephen M Irving Jan 06 '20 at 12:59
  • Also, the test method is going to be faster than the match method. Normally such micro-optimizations would not be important but since you are trying to initiate an alternate script during page load, optimization does matter in this instance. – Stephen M Irving Jan 06 '20 at 15:35
  • @StephenMIrving double bang is kinda semantics. Let's say if you're gonna be writing in typescript it will show you an error of wrong type being passed into if clause. So basically I do the type transformation manually. Also I'd say that `test` will be a better match here though also mostly for semantic purposes as optimizing microseconds here won't give you a lot of profit. This won't be running multiple times, so noone will notice such performance change. – Y.Puzyrenko Jan 07 '20 at 19:24
  • I have to disagree with you strongly on both counts. The double bang is useless smelly fluff that provides no value. If there is a purpose for it in Typescript that is irrelevant as we aren''t discussing Typescript code, this is JS and JS has implicit type coercion as a feature. There is no point to doing the type coercion manually here. Second, as I explained in my original answer, this code is being run while the page loads and is going to branch the scripting. Every ms that can be shaved off initial load time and the TTCP is valuable, and for companies that value is quantifiable in dollars. – Stephen M Irving Jan 07 '20 at 21:00
11

You define a boolean value with default of true, and then inside an IE conditional comment, set the value to false, and use the value of this to determine whether your advanced code should run. Something like:

<script type="text/javascript">var runFancy = true;</script>
<!--[if IE]>
<script type="text/javascript">
    runFancy = false;
    //any other IE specific stuff here
</script>
<![endif]-->
<script type="text/javascript">
    if (runFancy) {
         //do your code that works with sane browsers
    }
</script>
Ender
  • 14,995
  • 8
  • 36
  • 51
4

var browserName=navigator.appName; if (browserName=="Microsoft Internet Explorer") { document.write("Your html for IE") }

bren
  • 4,176
  • 3
  • 28
  • 43
  • For anyone who uses this answer, see this post first. IE 11 has some compatibility issues with this approach. http://stackoverflow.com/questions/17907445/how-to-detect-ie11 – Aaron Vanderwielen Aug 19 '14 at 18:14
1

Here is the script i used and it works like a charm. I used the boolean method Ender suggested as the other ones using only the IE specific script adds something to IE but doesn´t take the original code out.

    <script>runFancy = true;</script>
<!--[if IE]>
<script type="text/javascript">
    runFancy = false;
 </script> // <div>The HTML version for IE went here</div>
<![endif]-->

    // Below is the script used for all other browsers:
    <script src="accmenu/acac1.js" charset="utf-8" type="text/javascript"></script><script>ac1init_doc('',0)</script> 
dimmy4
  • 307
  • 1
  • 3
  • 5
1

Try this: The systemLanguage and the userLanguage is undefined in all browser.

if(navigator.userLanguage !== "undefined" && navigator.systemLanguage !== "undefined" && navigator.userAgent.match(/trident/i)) {
    alert("hello explorer i catch U :D")
  }
Péter Zajácz
  • 141
  • 2
  • 5
  • Why would you need the first two conditions? If you are correct that they are `"undefined"` in all browsers then those conditions would never be true. If they are true only for IE, they are still not necessary as `navigator.userAgent.match(/trident/)` will only return true for IE. – Stephen M Irving Jan 10 '20 at 17:04
0

Note that you can also determine in pure js in what browser you script is beeing executed through : window.navigator.userAgent

However, that's not a recommended way as it's configurable in the browser settings. More info available there: https://developer.mozilla.org/fr/docs/DOM/window.navigator.userAgent

Żabojad
  • 2,946
  • 2
  • 32
  • 39
-1

See this script in Microsoft's developer archives: https://msdn.microsoft.com/en-us/library/ms537509%28v=vs.85%29.aspx

I have used this script in quite a few projects, never had any problems.

basitmate
  • 81
  • 2
  • 9
-3

this code works well on my site because it detects whether its ie or not and activates the javascript if it is its below you can check it out live on ie or other browser Just a demo of the if ie javascript in action

<script type="text/javascript">
<!--[if IE]>
window.location.href = "http://yoursite.com/";
<![endif]-->
</script>