JavaScript to get the size of the browser window

Source: Internet
Author: User

Program Demonstration:

Obtains the current window size of the browser. When the browser window size changes, the displayed value changes at any time.

The program mainly uses some properties of the Document Object about the window. The main functions and usage of these properties are as follows:

  1. To obtain the Window size, different properties and methods are required for different browsers: to check the actual size of the Window, use the Window attribute in Netscape; in IE, you need to go deep into the Document to check the body. In DOM, if you want to get the size of the window, pay attention to the size of the root element
  2. The innerWidth attribute of the Window object contains the internal width of the current Window. The innerHeight attribute of the Window object contains the internal height of the current Window.
  3. The body attribute of the Document Object corresponds to the <body> tag of the HTML Document. The documentElement attribute of the Document Object indicates the root node of the HTML Document.
  4. Document. body. clientHeight indicates the current height of the window where the HTML document is located. Document. body. clientWidth indicates the current width of the window where the HTML document is located.
JavaScript Code
Var winWidth = 0; var winHeight = 0; function findDimensions () // function: Obtain the size {// obtain the window width if (window. innerWidth) winWidth = window. innerWidth; else if (document. body) & (document. body. clientWidth) winWidth = document. body. clientWidth; // obtain the window height if (window. innerHeight) winHeight = window. innerHeight; else if (document. body) & (document. body. clientHeight) winHeight = document. body. clientHeight; // detects the body in the Document to obtain the window size if (document.doc umentElement & document.doc umentElement. clientHeight & document.doc umentElement. clientWidth) {winHeight = document.doc umentElement. clientHeight; winWidth = document.doc umentElement. clientWidth;} // The result is output to two text boxes, document. form1.availHeight. value = winHeight; document. form1.availWidth. value = winWidth;} findDimensions (); // call the function to obtain the value window. onresize = findDimensions;

Procedure:

  1. The program first creates a form that contains two text boxes to display the current width and height of the window, and the value changes with the size of the window.
  2. In subsequent JavaScript code, two variables winWidth and winHeight are defined to save the height and width values of the window.
  3. Then, in the findDimensions () function, use window. innerHeight and window. innerWidth to get the height and width of the window, and save the two in the preceding two variables.
  4. Then, the body is detected in the Document, the window size is obtained, and stored in the preceding two variables.
  5. At the end of the function, access the form element by name and output the result to two text boxes.
  6. At the end of the JavaScript code, call the findDimensions () function to complete the entire operation.

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.