Home >Web Front-end >JS Tutorial >Is Using `navigator.appName` and `navigator.appVersion` Reliable for Detecting Older IE Versions?
Detect IE Version (Prior to v9) in JavaScript
Detection Criteria and Proposed Code
To determine if a user is accessing a website using a version of Internet Explorer prior to v9, the proposed code leverages the navigator.appName and navigator.appVersion properties.
if (navigator.appName.indexOf("Internet Explorer") != -1) { // User is using IE // Check for IE version < 9 }
Will This Code Do the Trick?
Despite addressing the concerns raised about user agent string spoofing and feature support detection, the proposed code has several limitations:
A Preferred Alternative
To overcome these limitations, a more comprehensive approach involves using CSS class-based detection:
html.lt-ie9 { ... } html.lt-ie8 { ... } html.lt-ie7 { ... }
<!--[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]-->
if ($('html').is('.lt-ie7, .lt-ie8, .lt-ie9')) { // IE version < 9 } else { // Other browsers }
This method provides more flexibility, compatibility, and control for handling different browser versions.
The above is the detailed content of Is Using `navigator.appName` and `navigator.appVersion` Reliable for Detecting Older IE Versions?. For more information, please follow other related articles on the PHP Chinese website!