JS obtains the screen, browser window size, and webpage height and width.

Source: Internet
Author: User

JS obtains the screen, browser window size, and webpage height and width.

This article describes how to use JS to obtain the screen, browser window size, and webpage height and width. For more information, see.

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

 

The Code is as follows:

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

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

</Html>

 

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.

 

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.