JS get browser window size get screen, browser, webpage height width

Source: Internet
Author: User
Visible area width of the webpage: Document. Body. clientwidth
Visible area height: Document. Body. clientheight
Visible area width of the webpage: Document. Body. offsetwidth (including the width of the edge)
Visible area height of the webpage: Document. Body. offsetheight (including the width of the edge)
Webpage text width: Document. Body. scrollwidth
Webpage text height: Document. Body. scrollheight
The height of the page to be rolled: Document. Body. scrolltop
Left: Document. Body. scrollleft
Page body: window. screentop
Page body part left: window. screenleft
Screen Resolution Height: window. Screen. Height
Screen Resolution width: window. Screen. Width
Available screen workspace Height: window. Screen. availheight
Available workspace width: window. Screen. availwidth

Precise HTML positioning: scrollleft, scrollwidth, clientwidth, and offsetwidth
Scrollheight: gets the scroll height of an object.
Scrollleft: sets or obtains the distance between the left-side border of the object and the leftmost end of the currently visible content in the window.
Scrolltop: sets or obtains the distance between the top of the object and the top of the visible content in the window.
Scrollwidth: gets the scroll width of an object.
Offsetheight: gets the height of an object relative to the layout or the parent coordinate specified by the parent coordinate offsetparent attribute.
Offsetleft: obtains the left position of the object relative to the layout or the parent coordinate specified by the offsetparent attribute.
Offsettop: obtains the top position of an object relative to the layout or the parent coordinate specified by the offsettop attribute.
The horizontal coordinates of event. clientx relative to the document
The vertical coordinates of event. clienty relative to the document
Horizontal coordinates of event. offsetx relative to the container
Event. offsety vertical coordinates of the container
Document.doc umentelement. scrolltop vertical scroll Value
Event.clientxw.document.doc umentelement. The horizontal coordinate of scrolltop relative to the document + The amount of vertical scrolling

The differences between IE and Firefox are as follows:

Ie6.0, ff1.06 +:

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

(The margin attribute in CSS is independent of clientwidth, offsetwidth, clientheight, and offsetheight)

Visible area width of the webpage: Document. Body. clientwidth
Visible area height: Document. Body. clientheight
Visible area width of the webpage: Document. Body. offsetwidth (including the width of the edge)
Visible area height of the webpage: Document. Body. offsetheight (including the height of the edge)
Webpage text width: Document. Body. scrollwidth
Webpage text height: Document. Body. scrollheight
The height of the page to be rolled: Document. Body. scrolltop
Left: Document. Body. scrollleft
Page body: window. screentop
Page body part left: window. screenleft
Screen Resolution Height: window. Screen. Height
Screen Resolution width: window. Screen. Width
Available screen workspace Height: window. Screen. availheight
Available screen workspace width: window. Screen. availwidth ------------------- 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, you need to pay attention to the size of the root element, instead of the element, to get the size of the window.

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 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

<! ---------------------------- File name: 30.3.htm ------------------------------>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en"

Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>

<HTML xmlns = "http://www.w3.org/1999/xhtml">

<Head>

<Title> adjust the browser window </title>

<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">

</Head>

<Body>

<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>

</Form>

<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.doc umentelement & document.doc umentelement. clientheight & document.doc umentelement. clientwidth)

{

Winheight = document.doc umentelement. clientheight;

Winwidth = document.doc umentelement. clientwidth;

}

// Output the result 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;
//-->

</script>

</body>

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.