Home > Web Front-end > JS Tutorial > How can I extract Firefox version information in Internet Explorer 6 using JavaScript?

How can I extract Firefox version information in Internet Explorer 6 using JavaScript?

Susan Sarandon
Release: 2024-11-16 02:32:02
Original
671 people have browsed it

How can I extract Firefox version information in Internet Explorer 6 using JavaScript?

Detecting Browser Version with JavaScript

Question:

The code provided doesn't retrieve the Firefox version number in Internet Explorer 6. How can I modify it to extract this information?

Answer:

The original code relies on browser-specific properties, such as navigator.appCodeName and navigator.appVersion. To ensure compatibility across different browsers, consider the following script:

var nVer = navigator.appVersion;
var nAgt = navigator.userAgent;
var browserName = navigator.appName;
var fullVersion = '' + parseFloat(navigator.appVersion);
var majorVersion = parseInt(navigator.appVersion, 10);
var nameOffset, verOffset, ix;

// Check for specific browsers
if ((verOffset = nAgt.indexOf("OPR")) != -1) {
  browserName = "Opera";
  fullVersion = nAgt.substring(verOffset + 4);
  if ((verOffset = nAgt.indexOf("Version")) != -1)
    fullVersion = nAgt.substring(verOffset + 8);
} else if ((verOffset = nAgt.indexOf("Edg")) != -1) {
  browserName = "Microsoft Edge";
  fullVersion = nAgt.substring(verOffset + 4);
} else if ((verOffset = nAgt.indexOf("MSIE")) != -1) {
  browserName = "Microsoft Internet Explorer";
  fullVersion = nAgt.substring(verOffset + 5);
} else if ((verOffset = nAgt.indexOf("Chrome")) != -1) {
  browserName = "Chrome";
  fullVersion = nAgt.substring(verOffset + 7);
} else if ((verOffset = nAgt.indexOf("Safari")) != -1) {
  browserName = "Safari";
  fullVersion = nAgt.substring(verOffset + 7);
  if ((verOffset = nAgt.indexOf("Version")) != -1)
    fullVersion = nAgt.substring(verOffset + 8);
} else if ((verOffset = nAgt.indexOf("Firefox")) != -1) {
  browserName = "Firefox";
  fullVersion = nAgt.substring(verOffset + 8);
} else if (
  (nameOffset = nAgt.lastIndexOf(" ") + 1) < (verOffset = nAgt.lastIndexOf("/"))
) {
  browserName = nAgt.substring(nameOffset, verOffset);
  fullVersion = nAgt.substring(verOffset + 1);
  if (browserName.toLowerCase() == browserName.toUpperCase()) {
    browserName = navigator.appName;
  }
}

// Trim version string
if ((ix = fullVersion.indexOf(";")) != -1) fullVersion = fullVersion.substring(0, ix);
if ((ix = fullVersion.indexOf(" ")) != -1) fullVersion = fullVersion.substring(0, ix);

majorVersion = parseInt('' + fullVersion, 10);
if (isNaN(majorVersion)) {
  fullVersion = '' + parseFloat(navigator.appVersion);
  majorVersion = parseInt(navigator.appVersion, 10);
}
Copy after login

This script uses heuristics to extract browser name and version information across different browsers, including Internet Explorer 6.

The above is the detailed content of How can I extract Firefox version information in Internet Explorer 6 using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template