SoFunction
Updated on 2025-03-01

JavaScript to obtain and judge the height, width of browser windows, screens, web pages, etc.

HTML precise attributes:scrollLeft,scrollWidth,clientWidth,offsetWidth

scrollHeight: Gets the scroll height of the object.
scrollLeft: Set or get the distance between the left boundary of the object and the leftmost end of the currently visible content in the window
scrollTop: Set or get the distance between the top of the object and the top of the visible content in the window
scrollWidth: Get the scroll width of the object
offsetHeight: Gets the height of the object relative to the layout or the parent coordinate specified by the parent coordinate offsetParent property
offsetLeft: Gets the calculated left position of the object relative to the layout or parent coordinates specified by the offsetParent property
offsetTop: Gets the calculated top position of the object relative to the layout or the parent coordinate specified by the offsetTop property
Horizontal coordinates relative to the document
Vertical coordinates relative to the document
Horizontal coordinates relative to the container
Vertical coordinates relative to container
The value of scrolling vertically
+ Horizontal coordinates relative to the document + the amount of scrolling in the vertical direction
IE, FireFox differences are as follows:
IE6.0、FF1.06+:

Copy the codeThe code is as follows:
clientWidth = width + padding
clientHeight = height + padding
offsetWidth = width + padding + border
offsetHeight = height + padding + border
IE5.0/5.5:
clientWidth = width – border
clientHeight = height – border
offsetWidth = width
offsetHeight = height


Tip: The margin attribute in CSS has nothing to do with clientWidth, offsetWidth, clientHeight, and offsetHeight

The visible area width of the web page:
The visible area height of the web page:
Width of visible area on the web page: (including the width of the edge line)
The visible area height of the web page: (including the height of the edge line)
The full text of the web page is:
The full text of the web page is high:
The web page is rolled out:
The left of the web page being rolled out:
On the main part of the page:
Left part of the web page text:
High screen resolution:
Width of screen resolution:
Screen Available Workspace Height:
Screen Available Workspace Width:


Technical Highlights

The code in this section mainly uses some properties of the Document object about the window. The main functions and usage of these properties are as follows.
To get the window size, different properties and methods need to be used for different browsers: to detect the true size of the window, you need to use the Window attributes under Netscape; to detect the body in IE; to get the window size in DOM environment, to get the window size, not the element.
The innerWidth property of the Window object contains the internal width of the current window. The innerHeight property of the Window object contains the internal height of the current window.
The body attribute of the Document object corresponds to the tag of the HTML document. The documentElement property of the Document object represents the root node of the HTML document.
Indicates the current height of the window where the HTML document is located. . clientWidth represents the current width of the window where the HTML document resides.

Sample code

Copy the codeThe code is as follows:

<!DOCTYPE>
<html>
    <head>
        <title>
Please adjust the browser window
        </title>
        <meta charset="utf8">
    </head>
    <body>
        <h2 align="center">
Please adjust the browser window size
        </h2>
        <hr>
        <form action="#" method="get" name="form1" >
<!--Show the actual size of the browser window-->
The actual height of the browser window:
            <input type="text" name="availHeight" size="4">
            <br>
Actual width of the browser window:
            <input type="text" name="availWidth" size="4">
            <br>
        </form>
        <script type="text/javascript">
            < !--
            var winWidth = 0;
            var winHeight = 0;
function findDimensions() //Function: Get the dimensions
            {
//Get window width
                if () winWidth = ;
                else if (() && ()) winWidth = ;
//Get window height
                if () winHeight = ;
                else if (() && ()) winHeight = ;
//Click the window size by detecting the body deep inside the Document
                if ( && && ) {
                    winHeight = ;
                    winWidth = ;
                }
//The result is output to two text boxes
                document. = winHeight;
                document. = winWidth;
            }
            findDimensions();
//Calling the function to get the value
            = findDimensions;
            //-->
        </script>
    </body>
</html>

Source program interpretation

(1) The program first creates a form containing two text boxes to display the current width and height of the window, and its value will change with the change of the window size.
(2) In the subsequent JavaScript code, two variables winWidth and winHeight are first defined to save the height and width values ​​of the window.
(3) Then, in the function findDimensions ( ), use and get the height and width of the window, and save both in the above two variables.
(4) Then, by going deep into the Document, detecting the body, obtaining the window size, and storing it in the above two variables.
(5) At the end of the function, the result is output to two text boxes by accessing the form element by name.
(6) At the end of the JavaScript code, complete the entire operation by calling the findDimensions ( ) function.