Get window size compatible with js Code of all browsers

Source: Internet
Author: User

Code for obtaining the window size:
Copy codeThe Code is as follows:
Var pageWidth = window. innerWidth,
Var pageHeight = window. innerHeight;
If (typeof pageWidth! = "Number "){
If (document. compatMode = "number "){
PageWidth = document.doc umentElement. clientWidth;
PageHeight = document.doc umentElement. clientHeight;
} Else {
PageWidth = document. body. clientWidth;
PageHeight = document. body. clientHeight;
}
}

We first pay the values of window. innerWidth and window. innerHeight respectively to pageWidth and pageHeight. Then, check whether the value saved in pageWidth is a numeric value. If not, use document. compatMode to determine whether the page is in standard mode. If yes, use the values of document.documentelement.clientwidthand document.doc umentElement. clientHeight separately. Otherwise, the values of document. body. clientWidth and document. body. clientHeight are used.
Code for obtaining the window position:
Copy codeThe Code is as follows:
Var leftPos = (typeof window. screenLeft = "number ")? Window. screenLeft: window. screenX;
Var topPos = (typeof window. screenTop = "number ")? Window. screenTop: window. screenY;
 
The two examples aim to obtain the left and top positions of the window. First, use the binary operator to determine whether the screenLeft and screenTops attributes exist. If they exist (in IE, Safari, Opera, and Chrome ), take the values of these two attributes. If it does not exist (in Firefox), take the screenX and screenY values.

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.