Home >Web Front-end >JS Tutorial >How Can JavaScript Accurately Detect Browser and Version?
Browser Detection in JavaScript
Determining the exact browser and version using JavaScript can be useful for a variety of purposes, such as providing customized experiences based on a user's browser capabilities. Here's how it can be achieved:
The navigator.saysWho() function is a JavaScript solution that provides a cross-browser method to detect and identify the exact browser and its version. It examines the userAgent string in the navigator object, which contains information about the browser being used.
The function begins by matching common browser signatures and versions against the userAgent string using regular expressions. If the match corresponds to a known browser, the function returns a string indicating the browser name and version.
For example, if the userAgent string contains "Chrome" and a version number, the function will return "Chrome" followed by the version number. Similarly, if "MSIE" or "trident" is detected, the function will return "IE" followed by the version number.
The navigator.saysWho() function is also able to detect less common browsers, such as Opera and Edge Chromium, by matching their respective signatures and version numbers.
To use the function, simply call navigator.saysWho() to retrieve the browser name and version as a string.
console.log(navigator.saysWho()); // outputs: "Chrome 89"
The above is the detailed content of How Can JavaScript Accurately Detect Browser and Version?. For more information, please follow other related articles on the PHP Chinese website!