I want to bounce users of our web site to an error page if they're using a version of Internet Explorer
prior to v9. It's just not worth our time and money to support IE pre-v9
. Users of all other non-IE browsers are fine and shouldn't be bounced. Here's the proposed code:
if(navigator.appName.indexOf("Internet Explorer")!=-1){ //yeah, he's using IE
var badBrowser=(
navigator.appVersion.indexOf("MSIE 9")==-1 && //v9 is ok
navigator.appVersion.indexOf("MSIE 1")==-1 //v10, 11, 12, etc. is fine too
);
if(badBrowser){
// navigate to error page
}
}
Will this code do the trick?
To head off a few comments that will probably be coming my way:
useragent
string. I'm not concerned.pre-v9 IE
browsers don't. Checking feature by feature throughout the site would be a waste.IE v1
(or >= 20) wouldn't get 'badBrowser' set to true and the warning page wouldn't be displayed properly. That's a risk we're willing to take.IE 10
, rendering this approach absolutely useless.Any other obvious issues to be aware of?
This is my preferred way of doing it. It gives maximum control. (Note: Conditional statements are only supported in IE5 - 9.)
First set up your ie classes correctly
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html> <!--<![endif]-->
<head>
Then you can just use CSS to make style exceptions, or, if you require, you can add some simple JavaScript:
(function ($) {
"use strict";
// Detecting IE
var oldIE;
if ($('html').is('.lt-ie7, .lt-ie8, .lt-ie9')) {
oldIE = true;
}
if (oldIE) {
// Here's your JS for IE..
} else {
// ..And here's the full-fat code for everyone else
}
}(jQuery));
Thanks to Paul Irish.