JS get page window size explanation

Source: Internet
Author: User

Technical Points

This section describes how to obtain the current window size of a browser. When the browser window size changes, the displayed value changes at any time.

Technical Points

This section uses the window attributes of the Document Object. The main functions and usage of these attributes are as follows.

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 <HTML> instead of the <body> element.

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.

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.

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.

Implementation Code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<Title> adjust the browser window </title>
    <meta http-equiv="content-type" content="text/html; charset=gb2312">
<H2 align = "center"> adjust the browser window size </H2> <HR>
    <form action="#" method="get" name="form1" id="form1">
<! -- Display the actual size of the browser window -->
Actual browser window height: <input type = "text" name = "availheight" size = "4"> <br>
Actual browser window width: <input type = "text" name = "availwidth" size = "4"> <br>
    <script type="text/javascript">
        var winWidth = 0;
        var winHeight = 0;
Function finddimensions () // function: obtains 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.documentElement  && document.documentElement.clientHeight &&
                 winHeight = document.documentElement.clientHeight;
                 winWidth = document.documentElement.clientWidth;
// Output the result to two text boxes
             document.form1.availHeight.value= winHeight;
             document.form1.availWidth.value= winWidth;
Finddimensions (); // call a function to obtain a value

After the program is run, two text boxes are displayed, showing the current width and height of the window, 30.7 respectively. When the browser window size is adjusted, the value in the text box changes accordingly. The values shown in Figure 30.8, figure 30.9, and figure 30.10 are the size of the window at different times.

Figure 30.7 code 30.3.htm display result figure 30.8 display result after widening

Figure 30.9 Display Results After height increase figure 30.10 Display Results After width and height

Source program explanation

(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) Check the body in the document to obtain the window size and store it 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, complete the operation by calling the finddimensions () function.

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.