Javascript-based window, screen, and navigator objects

Source: Internet
Author: User

[Html]
A) Let's first look at several coordinate systems:
1> the screen coordinates describe the location of a browser window on the desktop, which is measured relative to the upper left corner of the desktop.
2> the window coordinates describe the positions of the video ports in the web browser. They are measured relative to the upper left corner of the view.
3> the coordinates in the document describe the positions in an HTML document. They are measured relative to the upper left corner of the document. Document coordinates
 
 
Obtain the window location:
Var getWinPosition = function (){
Var pos = {
// Window. screenX => FF
Left :( typeof window. screenLeft = 'number ')? Window. screenLeft: window. screenX;
Top :( typeof window. screenTop = "number ")? Window. screenTop: window. screenY;
};
Return pos;
}
Get the Page View Size:
Var getViewPortSize = function (){
Var w = window. innerWidth,
H = widnow. innerHeight;
If (typeof w! = "Number") {// IE
If (document. compatMode = "CSS1Compat") {// standard mode
W = document.doc umentElement. clientWidth;
H = document.doc umentElement. clientHeight;
} Else {// none in IE6 mixed mode <! Doctype> Declaration
W = document. body. clientWidth;
H = document. body. clientHeight;
}
}
Return {
Width: w,
Height: h
}
}
 
B) The screen Object provides information on the display size and color quantity.
Attribute description
AvailHeight screen height (excluding the taskbar)
AvailWidth screen width (excluding the taskbar)
BufferDepth: sets or obtains the number of pixels in the bitmap buffer color.
ColorDepth gets the number of pixel digits used for the color of the target setting or buffer.
DeviceXDPI: sets or obtains the actual point value per inch in the horizontal direction of the System screen.
DeviceYDPI: sets or obtains the actual point value per inch in the vertical direction of the System screen.
FontSmoothingEnabled: Gets whether the user has enabled the option of rounding the font corners in the display settings
Height: obtains the vertical resolution of the screen.
LogicalXDPI
LogicalYDPI: obtains the regular point value of the system screen vertical per inch
UpdateInterval
Width: obtains the vertical resolution of the screen.
 
Obtain the screen size (excluding the taskbar:
Var getScreenSize = function (){
Var w = screen. availWidth,
H = screen. availHeight;
Return {
Width: w,
Height: h
}
}
 
C) The Navigator object contains all information about the browser.
Var getBrowserMessage = function (){
Var nav = navigator;
Return "appName:" + nav. appName + "\ n" + // simple browser name
"AppCodeName:" + nav. appCodeName + "\ n" + // browser code name
"AppVersion:" + nav. appVersion + "\ n" + // browser version
"UserAgent:" + nav. userAgent + "\ n" + // string sent by the browser in the user-agent HTTP Header
"Platform:" + nav. platform; // hardware platform for running the browser
}
 
// FF
AppName: Netscape
AppCodeName: Mozilla
AppVersion: 5.0 (Windows)
UserAgent: Mozilla/5.0 (Windows NT 5.1; rv: 21.0) Gecko/20100101 Firefox/21.0
Platform: Win32
 
Var pattern =/(mozilla )(? :.*? Rv :( [\ w.] +) | )/;
// IE6.0
AppName: Microsoft Internet Explorer
AppCodeName: Mozilla
AppVersion: 4.0 (compatible; MSIE 6.0; Windows NT 5.1; InfoPath.2;. NET4.0C)
UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; InfoPath.2;. NET4.0C)
Platform: Win32
 
Var pattern =/(msie) ([\ w.] + )/;
// Chorme
AppName: Netscape
AppCodeName: Mozilla
AppVersion: 5.0 (Windows NT 5.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.20.1.97 Safari/537.11
UserAgent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.20.1.97 Safari/537.11
Platform: Win32
 
Var pattern =/(chrome) [\/] ([\ w.] + )/;
// Opera
AppName: Opera
AppCodeName: Mozilla
AppVersion: 9.80 (Windows NT 5.1; U; zh-cn)
UserAgent: Opera/9.80 (Windows NT 5.1; U; zh-cn) Presto/2.10.289 Version/12.02
Platform: Win32
 
Var pattern =/(opera )(? :. * Version |) [\/] ([\ w.] + )/;
// Safari
AppName: Netscape
AppCodeName: Mozilla
AppVersion: 5.0 (Windows NT 5.1) AppleWebKit/534.54.16 (KHTML, like Gecko) Version/5.1.4 Safari/534.54.16
UserAgent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.54.16 (KHTML, like Gecko) Version/5.1.4 Safari/534.54.16
Platform: Win32
 
Var pattern =/(webkit) [\/] ([\ w.] + )/;
 
Get the browser version:
Var getBrowser = function (ua ){
Ua = ua. toLowerCase ();
Var match =/(chrome) [\/] ([\ w.] +)/. exec (ua) |
/(Webkit) [\/] ([\ w.] +)/. exec (ua) |
/(Opera )(? :. * Version |) [\/] ([\ w.] +)/. exec (ua) |
/(Msie) ([\ w.] +)/. exec (ua) |
Ua. indexOf ("compatible") <0 &/(mozilla )(? :.*? Rv :( [\ w.] +) |)/. exec (ua) |
[];
 
Return {
Browser: match [1] | "",
Version: match [2] | "0"
};
};
Var nav = getBrowser (navigator. userAgent );
Alert (nav. browser + "----" + nav. version );
 
D) return to the top
Window. location. hash = "# top ";
Or
Window. location. replace ("# top ");

A) Let's first look at several coordinate systems:
1> the screen coordinates describe the location of a browser window on the desktop, which is measured relative to the upper left corner of the desktop.
2> the window coordinates describe the positions of the video ports in the web browser. They are measured relative to the upper left corner of the view.
3> the coordinates in the document describe the positions in an HTML document. They are measured relative to the upper left corner of the document. Document coordinates


Obtain the window location:
Var getWinPosition = function (){
Var pos = {
// Window. screenX => FF
Left :( typeof window. screenLeft = 'number ')? Window. screenLeft: window. screenX;
Top :( typeof window. screenTop = "number ")? Window. screenTop: window. screenY;
};
Return pos;
}
Get the Page View Size:
Var getViewPortSize = function (){
Var w = window. innerWidth,
H = widnow. innerHeight;
If (typeof w! = "Number") {// IE
If (document. compatMode = "CSS1Compat") {// standard mode
W = document.doc umentElement. clientWidth;
H = document.doc umentElement. clientHeight;
} Else {// none in IE6 mixed mode <! Doctype> Declaration
W = document. body. clientWidth;
H = document. body. clientHeight;
}
}
Return {
Width: w,
Height: h
}
}

B) The screen Object provides information on the display size and color quantity.
Attribute description
AvailHeight screen height (excluding the taskbar)
AvailWidth screen width (excluding the taskbar)
BufferDepth: sets or obtains the number of pixels in the bitmap buffer color.
ColorDepth gets the number of pixel digits used for the color of the target setting or buffer.
DeviceXDPI: sets or obtains the actual point value per inch in the horizontal direction of the System screen.
DeviceYDPI: sets or obtains the actual point value per inch in the vertical direction of the System screen.
FontSmoothingEnabled: Gets whether the user has enabled the option of rounding the font corners in the display settings
Height: obtains the vertical resolution of the screen.
LogicalXDPI
LogicalYDPI: obtains the regular point value of the system screen vertical per inch
UpdateInterval
Width: obtains the vertical resolution of the screen.

Obtain the screen size (excluding the taskbar:
Var getScreenSize = function (){
Var w = screen. availWidth,
H = screen. availHeight;
Return {
Width: w,
Height: h
}
}

C) The Navigator object contains all information about the browser.
Var getBrowserMessage = function (){
Var nav = navigator;
Return "appName:" + nav. appName + "\ n" + // simple browser name
"AppCodeName:" + nav. appCodeName + "\ n" + // browser code name
"AppVersion:" + nav. appVersion + "\ n" + // browser version
"UserAgent:" + nav. userAgent + "\ n" + // string sent by the browser in the user-agent HTTP Header
"Platform:" + nav. platform; // hardware platform for running the browser
}

// FF
AppName: Netscape
AppCodeName: Mozilla
AppVersion: 5.0 (Windows)
UserAgent: Mozilla/5.0 (Windows NT 5.1; rv: 21.0) Gecko/20100101 Firefox/21.0
Platform: Win32

Var pattern =/(mozilla )(? :.*? Rv :( [\ w.] +) | )/;
// IE6.0
AppName: Microsoft Internet Explorer
AppCodeName: Mozilla
AppVersion: 4.0 (compatible; MSIE 6.0; Windows NT 5.1; InfoPath.2;. NET4.0C)
UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; InfoPath.2;. NET4.0C)
Platform: Win32

Var pattern =/(msie) ([\ w.] + )/;
// Chorme
AppName: Netscape
AppCodeName: Mozilla
AppVersion: 5.0 (Windows NT 5.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.20.1.97 Safari/537.11
UserAgent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.20.1.97 Safari/537.11
Platform: Win32

Var pattern =/(chrome) [\/] ([\ w.] + )/;
// Opera
AppName: Opera
AppCodeName: Mozilla
AppVersion: 9.80 (Windows NT 5.1; U; zh-cn)
UserAgent: Opera/9.80 (Windows NT 5.1; U; zh-cn) Presto/2.10.289 Version/12.02
Platform: Win32

Var pattern =/(opera )(? :. * Version |) [\/] ([\ w.] + )/;
// Safari
AppName: Netscape
AppCodeName: Mozilla
AppVersion: 5.0 (Windows NT 5.1) AppleWebKit/534.54.16 (KHTML, like Gecko) Version/5.1.4 Safari/534.54.16
UserAgent: Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.54.16 (KHTML, like Gecko) Version/5.1.4 Safari/534.54.16
Platform: Win32

Var pattern =/(webkit) [\/] ([\ w.] + )/;

Get the browser version:
Var getBrowser = function (ua ){
Ua = ua. toLowerCase ();
Var match =/(chrome) [\/] ([\ w.] +)/. exec (ua) |
/(Webkit) [\/] ([\ w.] +)/. exec (ua) |
/(Opera )(? :. * Version |) [\/] ([\ w.] +)/. exec (ua) |
/(Msie) ([\ w.] +)/. exec (ua) |
Ua. indexOf ("compatible") <0 &/(mozilla )(? :.*? Rv :( [\ w.] +) |)/. exec (ua) |
[];

Return {
Browser: match [1] | "",
Version: match [2] | "0"
};
};
Var nav = getBrowser (navigator. userAgent );
Alert (nav. browser + "----" + nav. version );

D) return to the top
Window. location. hash = "# top ";
Or
Window. location. replace ("# top ");

 

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.