最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to redirect Internet Explorer users to a new page? - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question asked Oct 7, 2014 at 14:13 TomTom 4461 gold badge6 silver badges14 bronze badges 2
  • msdn.microsoft./en-us/library/ms537512(v=vs.85).aspx – j08691 Commented Oct 7, 2014 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 Commented Oct 7, 2014 at 14:19
Add a ment  | 

4 Answers 4

Reset to default 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

I know there have been answers all around, but here is in my opinion the most plete 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

You need to use conditional ments.

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

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

Try this:

if (window.navigator.userAgent.indexOf("MSIE ") > 0)
    window.location="error.html";
发布评论

评论列表(0)

  1. 暂无评论