Problem:
You want to redirect users of your website to an error page if they're using a version of Internet Explorer (IE) prior to version 9.
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 } }
Preferred Solution:
While the proposed code might work under certain conditions, it's not the most reliable or efficient approach. A better method is to use conditional HTML comments, along with JavaScript, to detect and handle different browser versions.
HTML:
<!DOCTYPE html> <!--[if lt IE 7]> <html>
This code creates classes for IE versions 7, 8, and below 9, allowing you to easily target and style those versions.
JavaScript:
(function ($) { "use strict"; // Detecting IE var oldIE; if ($('html').is('.lt-ie7, .lt-ie8, .lt-ie9')) { oldIE = true; } if (oldIE) { // Handle IE-specific code here } else { // Handle code for all other browsers } }(jQuery));
This code sets the oldIE variable to true if the browser is IE version 7 or below. You can then use conditional statements to execute different code depending on the browser version.
This method is more reliable and adaptable, allowing you to handle specific browser versions or feature support requirements more effectively.
The above is the detailed content of How Do I Detect Internet Explorer Versions (Prior to v9) in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!