Reputation: 446
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
Upvotes: 2
Views: 6917
Reputation: 532
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
Upvotes: 2
Reputation: 730
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
Upvotes: 6
Reputation: 624
Try this:
if (window.navigator.userAgent.indexOf("MSIE ") > 0)
window.location="error.html";
Upvotes: -1
Reputation: 2777
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]-->
Upvotes: 0