JavaScript FAQ (22)--Client information __java

Source: Internet
Author: User

19. Client Information

1, browser name (Browser name)

Q: How do I detect the browser name.

A: to get the actual name of the user's browser, you can use the Navigator.appname and Navigator.useragent properties. The UserAgent property is more reliable than appname, for example, Firefox (and some other browsers) may return "Netscape" to Navigator.appname because it is compatible with Netscape Navigator.

The following code instance uses Navigator.useragent to implement browser detection. At the same time, it also uses navigator.appname and navigator.appversion, only as a reference when UserAgent returns an unexpected form. The following is the output in your browser:

The source code for the browser detection in the example is:

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; In MSIE, the true version are after "MSIE" in UserAgent if ((Veroffset=nagt.indexof ("MSIE"))!=-1) {browsername = "MICR
 Osoft Internet Explorer ";
Fullversion = nagt.substring (veroffset+5); //In Opera, the true version was after "opera" Else if ((Veroffset=nagt.indexof ("opera"))!=-1 {browsername = "opera"
 ;
Fullversion = nagt.substring (veroffset+6); /In Chrome, the true version was after "Chrome" else if ((Veroffset=nagt.indexof ("Chrome")!=-1) {browsername = "Chr
 Ome ";
Fullversion = nagt.substring (veroffset+7); //In Safari, the true version was after "Safari" else if ((Veroffset=nagt.indexof ("Safari"))!=-1 {browsername = "Saf
 Ari ";
Fullversion = nagt.substring (veroffset+7); //In Firefox, true version are after "Firefox" 
else if ((Veroffset=nagt.indexof ("Firefox"))!=-1 {browsername = "Firefox";
Fullversion = nagt.substring (veroffset+8); }//In most other browsers, "Name/version" was at the end of useragent 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 the fullversion string at semicolon/space if present 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);
} document.write (' Browser name = ' +browsername+ ' <br> '); document.write (' full version = ' +fullversion+ ' <br> ');
document.write (' Major version = ' +majorversion+ ' <br> ');
document.write (' navigator.appname = ' +navigator.appname+ ' <br> ');
 document.write (' navigator.useragent = ' +navigator.useragent+ ' <br> ');

2, browser versions (Browser version)

Q: How to detect the browser version.

A: Unfortunately, navigator.appversion is not good enough to detect the browser version. For example, in many browsers that support JavaScript, the value of Navigator.appversion is compatible with the version of Netscape Navigator, not the actual version of the user's browser. (even in the early days of JavaScript applications, the return value of Microsoft Internet Explorer 3 navigator.appversion was 2, meaning it was compatible with Netscape Navigator2.) )

Therefore, to get the full version of any of the above browsers, you need to rely on the navigator.useragent return value, as in the case of the browser name. Here is the name and version of your browser:

(Source code see article browser name)

3. Operating system (operating system)

Q: How to detect the operating system on a client computer.

A: to detect the operating system on the client, the script needs to parse the value of the navigator.appversion. The following is a simple example that assigns the name of the operating system to the Osname variable.

This script sets Osname variable as follows:
//Windows to all    versions to Windows
//"MacOS" for      al L versions of Macintosh OS
//"Linux" for all      versions to Linux
//"UNIX" for all other       Unix flavors 
/ /"Unknown OS" indicates failure to detect the OS

var osname= "Unknown os";
if (Navigator.appVersion.indexOf ("Win")!=-1) osname= "Windows";
if (Navigator.appVersion.indexOf ("Mac")!=-1) osname= "MacOS";
if (Navigator.appVersion.indexOf ("X11")!=-1) osname= "UNIX";
if (Navigator.appVersion.indexOf ("Linux")!=-1) osname= "Linux";

document.write (' Your OS: ' +osname);

On your operating system, the result of this script is:

(For more detailed operating system information, scripts can do more complex analysis of navigator.appversion, but the rationale is the same.) )

4, screen size (screens size)

Q: How to obtain the client's screen size.

A: to detect the size of the client's screen, you can use the properties Screen.width and Screen.height, which are supported on the 4 version of the main browser. If your users are using Netscape Navigator 3 and Java enabled, you can use Java calls to get the width and height of the screen (see example below).

The following code assigns the actual width and height of the screen to the variable screenw and screenh, and then outputs their values. If the user is using an older version of the browser, then SCREENW and sreenh are assigned values of 640 and 480 respectively.

var screenw = 640, screenh =;
if (parseint (navigator.appversion) >3) {
 screenw = screen.width;
 Screenh = screen.height;
}
else if (navigator.appname = "Netscape" 
    && parseint (navigator.appversion) ==3
    && Navigator.javaenabled ()
   ) 
{
 var jtoolkit = Java.awt.Toolkit.getDefaultToolkit ();
 var jscreensize = Jtoolkit.getscreensize ();
 Screenw = Jscreensize.width;
 Screenh = jscreensize.height;
}

document.write (
 "screen width =" +screenw+ "<br>"
+ "screen height =" +screenh
)

In your browser, the output of the code is:

5. browser window size (Browser Windows size)

Q: How to get the size of the browser window.

A: to determine the actual size of the browser window, you can use the following properties in Netscape Navigator 4: Window.innerwidth, Window.innerheight on the Microsoft Internet Explorer: Document.body.offsetWidth, Document.body.offsetHeight

Note that DOCUMENT.BODY.OFFSETWIDHT and document.body.offsetHeight must be executed after the browser load <BODY> tag completes.

In the following code, WINW and Winh are assigned the width and height of the actual browser window and then output. If users use older browsers, then WINW and Winh will be set to 630 and 460, respectively.

var winw = 630, Winh = 460;

if (parseint (navigator.appversion) >3) {
 if (navigator.appname== "Netscape") {
  winw = window.innerwidth;
  Winh = window.innerheight;
 }
 if (Navigator.appName.indexOf ("Microsoft")!=-1) {
  winw = document.body.offsetWidth;
  Winh = document.body.offsetHeight;
 }

document.write (
 "window width =" +winw+ "<br>"
+ "window height =" +winh
)

In your browser, the code performs the following results:

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.