2

I have tried using the following script

[if IE]
<script type="text/javascript">
window.location = "error.html";
</script>
[endif]

It works like a treat apart from the fact that other browsers such as Chrome are also redirecting to the error.html page. What is wrong with it? Thanks

Tom
  • 446
  • 1
  • 5
  • 14
  • http://msdn.microsoft.com/en-us/library/ms537512(v=vs.85).aspx – j08691 Oct 07 '14 at 14:16
  • 2
    Don't. Seriously. If people want to use IE, especially modern IE. Let them. Use feature detection and progressive enhancement instead. – Quentin Oct 07 '14 at 14:19

4 Answers4

6

Try this:

<script type="text/javascript">
if(navigator.appName.indexOf("Internet Explorer")!=-1 || navigator.userAgent.match(/Trident.*rv[ :]*11\./))
{
   //This user uses Internet Explorer
   window.location = "error.html";
}
</script>

Greetings from Vienna

Bernd
  • 730
  • 1
  • 5
  • 13
  • 1
    That won't match IE 11. http://blogs.msdn.com/b/ieinternals/archive/2013/09/21/internet-explorer-11-user-agent-string-ua-string-sniffing-compatibility-with-gecko-webkit.aspx – Quentin Oct 07 '14 at 14:18
  • Now it thinks Chrome is IE11 – Quentin Oct 07 '14 at 14:26
  • It would appear not to be working. Just to clarify, where is the error.html supposed to be put? – Tom Oct 07 '14 at 14:52
2

I know there have been answers all around, but here is in my opinion the most complete answer..

HTML

<p>Is this internet explorer?<p>
<p id="ie"></p>

And now the JavaScript

if(detectIE()){
  document.getElementById("ie").innerHTML = "Yes it is!";
} else {
  document.getElementById("ie").innerHTML = "No it's not!";
}

function detectIE() {
  var ua = window.navigator.userAgent;

  var msie = ua.indexOf('MSIE ');
   if (msie > 0) {
     return true;
   }

  var trident = ua.indexOf('Trident/');
   if (trident > 0) {
    return true;
   }

  var edge = ua.indexOf('Edge/');
  if (edge > 0) {
    return true;
  }

  // other browser
  return false;
}

Working example: https://codepen.io/gerritman123/pen/VjrONQ

Gerrit Luimstra
  • 522
  • 6
  • 23
0

You need to use conditional comments.

Keep in mind though, IE10+ does not respect these conditional comments and will treat them the same way Firefox and Chrome does.

<!--[if IE]>
<script type="text/javascript">
window.location = "error.html";
</script>
<![endif]-->
Jmh2013
  • 2,625
  • 2
  • 26
  • 42
-1

Try this:

if (window.navigator.userAgent.indexOf("MSIE ") > 0)
    window.location="error.html";
mailmindlin
  • 616
  • 2
  • 7
  • 25
  • That won't match IE 11. http://blogs.msdn.com/b/ieinternals/archive/2013/09/21/internet-explorer-11-user-agent-string-ua-string-sniffing-compatibility-with-gecko-webkit.aspx – Quentin Oct 07 '14 at 14:19