The Navigator object contains basic information about the Web browser (such as name, version, operating system, etc.)
You can refer to the object by Window.navigator and use its properties to read the client base information
5 Main properties of Navigator:
Name of the Appname:web browser
AppVersion: Browser version number and other version information
UserAgent: The string that the browser sends in its User-agent HTTP header. This property contains all the information for the Appname,appversion property
appCodeName: The code name of the browser
Platform: The operating system where the client browser resides
<! DOCTYPE html PUBLIC "-// W3C // DTD XHTML 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv = "Content-Type" content = "text / html; charset = utf-8" />
<title> navigator object </ title>
</ head>
<body>
<script type = "text / javascript">
var appname = navigator.appName;
var appversion = navigator.appVersion;
var useragent = navigator.userAgent;
var appcodename = navigator.appCodeName;
var platform = navigator.platform;
document.write (appname + "<br>" + appversion + "<br>" + useragent + "<br>" + appcodename + "<br>" + platform);
</ script>
</ body>
</ html>
Common browser detection methods:
Feature detection method
This is a detection method that is determined to know which specific information of the browser or which specific function is supported.
Imprecise, but the safest. We just need to know if it exists or not.
For example, we only need if (navigator.appName.indexOf ("Netscape")! =-1) {***}
Instead of outputting specific browser name results
2. String detection method
This is more formal, although detecting the browser model and type is difficult and prone to errors.
First, detect the type and version of the browser:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>navigator
object
</title>
</head>
<body>
<script type="text/javascript">
var ua = navigator.userAgent.toLowerCase();
var info={
ie:/msie/.test(ua)&&!/opera/.test(ua),
op:/opera/.test(ua),
sa:/version.*safari/.test(ua),
ch:/chrome/.test(ua),
ff:/gecko/.test(ua)&&!/webkit/.test(ua)
};
(info.ie)&&alert("IE");
(info.ch)&&alert("Chrome");
(info.ff)&&alert("Firefox");
</script>
</body>
</html>
Second, the design function to obtain the IE version number:
There are many versions of IE. With the increase of compatibility problems, we also need to obtain the number of IE version. In this way, when writing a web page, you can adjust the response of different IE versions to show the same effect, and avoid compatibility problems. Caused by display problems.
<! DOCTYPE html PUBLIC "-// W3C // DTD XHTML 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv = "Content-Type" content = "text / html; charset = utf-8" />
<title> navigator object </ title>
</ head>
<body>
<script type = "text / javascript">
function getIEVer ()
{
var ver = navigator.userAgent; // Get the web browser version number of the client
var a = ver.indexOf ("MSIE"); // Detect the position of the special string "MSIE", because MSIE always exists in the version information of IE, which is common
if (a <0) {return 0;}
return parseFloat (ver.substring (a + 5, ver.indexOf (";", a)));
}
alert (getIEVer ());
</ script>
</ body>
</ html>
I tried it on IE11 just now, it returned 0, and then alert (navigation.appVersion) found that it was no longer in the previous format, indicating that it was innocent. Can only use other methods to detect the version number of IE11.
Third, detect the client operating system
var isWin = (navigator.userAgent.indexOf ("Win")! =-1)
// If it is a windows operating system, return TRUE
The others are "Mac", "X11", and "Linux" are Macintosh, UNIX, and Linux respectively.