Understand the Navigator object in JavaScript and detect browser type and version, get browser version number, detect client operating system __java

Source: Internet
Author: User


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.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.