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

Source: Internet
Author: User

Web page Visible area width: document.body.clientWidth

Web page Visible Area height: document.body.clientHeight

Web page Visible Area width: document.body.offsetWidth (including edge width)

Web page Visible Area High: document.body.offsetHeight (including edge height)

Page body Full text width: document.body.scrollWidth

Page body Full text High: Document.body.scrollHeight

Page is rolled away High: document.body.scrollTop

Webpage is rolled away left: document.body.scrollLeft

Page body part: Window.screentop

Page body part left: Window.screenleft

High screen resolution: Window.screen.height

Width of screen resolution: Window.screen.width

Screen available work area height: window.screen.availHeight

Screen available work area width: window.screen.availWidth

HTML Precise positioning: Scrollleft,scrollwidth,clientwidth,offsetwidth

ScrollHeight: Gets the scroll height of the object.

ScrollLeft: Sets or gets the distance between the left edge of the object and the leftmost of the currently visible content in the window

ScrollTop: Sets or gets the distance between the top of the object and the top of the visible content in the window

ScrollWidth: Gets the scrolling width of the object

Offsetheight: Gets the object relative to the canvas or by the parent coordinate

The height of the parent coordinate specified by the OffsetParent property

Offsetleft: Gets the object relative to the layout or by the

The OffsetParent property specifies the parent coordinate of the computed left position

OffsetTop: Gets the object relative to the layout or by the

The OffsetTop property specifies the top position of the calculated parent coordinate

Event.clientx horizontal coordinates of relative documents

Event.clienty vertical coordinates of relative documents

Event.offsetx horizontal coordinates relative to the container

Event.offsety the vertical coordinate of the relative container

Document.documentElement.scrollTop The vertical scrolling value

Event.clientx+document.documentelement.scrolltop horizontal coordinates of relative documents + amount of vertical scrolling

Ie,firefox differences 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

(Need to mention: the Margin property in CSS, and clientwidth, Offsetwidth, clientheight, offsetheight are irrelevant)

Web page Visible area width: document.body.clientWidth

Web page Visible Area height: document.body.clientHeight

Web page Visible Area width: document.body.offsetWidth (including edge width)

Web page Visible Area High: document.body.offsetHeight (including edge height)

Page body Full text width: document.body.scrollWidth

Page body Full text High: Document.body.scrollHeight

Page is rolled away High: document.body.scrollTop

Webpage is rolled away left: document.body.scrollLeft

Page body part: Window.screentop

Page body part left: Window.screenleft

High screen resolution: Window.screen.height

Width of screen resolution: Window.screen.width

Screen available work area height: window.screen.availHeight

Screen available work area width: window.screen.availWidth

-------------------

Technical Essentials

This section of the code mainly uses the Document object about the properties of the window, the main functions and usage of these properties are as follows.

To get the size of the window, you need to use different properties and methods for different browsers: to detect the real size of the window, you need to use window's properties under Netscape, and you need to examine the body inside the document under IE;

In the DOM environment, to get the size of the window, you need to be aware of the size of the root element, not the element.

The Innerwidth property of a Window object contains the internal width of the current window.

The Innerheight property of a Window object contains the internal height of the current window.

The body attribute of the Document object corresponds to the label of the HTML document.

The DocumentElement property of the Document object represents the root node of the HTML documents.

Document.body.clientHeight represents the current height of the window in which the HTML document resides.

Document.body. clientwidth represents the current width of the window in which the HTML document resides.

Implementation code? [Copy to Clipboard] Download 52004.txt

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<title> Adjust your browser window </title>

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "> </meta>

<body>

<H2 align= "center" > Please resize the browser window

<form action= "#" method= "Get" Name= "Form1" Id= "Form1" >

<!--show the actual size of the browser window--

Actual height of browser window: <input type= "text" name= "availheight" size= "4"/><br/>

Actual width of browser window: <input type= "text" name= "availwidth" size= "4"/><br/>

</form>

<script type= "Text/javascript" >

<!--

var winwidth = 0;

var winheight = 0;

function finddimensions ()//functions: Get dimension {//Get window width

if (window.innerwidth)

Winwidth = window.innerwidth;

else if ((document.body) && (document.body.clientWidth))

Winwidth = Document.body.clientWidth; Get window Height

if (window.innerheight)

Winheight = Window.innerheight;

else if ((document.body) && (document.body.clientHeight))

Winheight = Document.body.clientHeight; Get the window size by examining the body inside the document

if (document.documentelement &&

Document.documentElement.clientHeight &&

Document.documentElement.clientWidth)

{

Winheight = Document.documentElement.clientHeight;

Winwidth = Document.documentElement.clientWidth;

}//Results output to two text boxes

Document.form1.availheight.value= Winheight;

Document.form1.availwidth.value= Winwidth;

}

Finddimensions (); Call a function to get a value

Window.onresize=finddimensions;

-

</script>

</body>

Source program Interpretation

(1) The program first establishes a form that contains two text boxes that display the current width and height of the window, and that its value varies with the size of the window.

(2) in the subsequent JavaScript code, two variables winwidth and winheight are defined first to hold the window's height and width values.

(3) Then, in function finddimensions (), use Window.innerheight and window.innerwidth to get the height and width of the window and save both in the two variables described above.

(4) By deep inside the document to detect the body, to get the window size, and stored 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.

Page position and window size

function GetPageSize () {

var scrw, SCRH;

if (window.innerheight && window.scrollmaxy) {//Mozilla

SCRW = window.innerwidth + Window.scrollmaxx;

SCRH = Window.innerheight + window.scrollmaxy;

}

else if (Document.body.scrollHeight > Document.body.offsetHeight) {//all but IE Mac

SCRW = Document.body.scrollWidth;

SCRH = Document.body.scrollHeight;

} else if (document.body) {//IE Mac

SCRW = Document.body.offsetWidth;

SCRH = Document.body.offsetHeight;

}

var winw, Winh;

if (window.innerheight) {//all except IE

WINW = window.innerwidth;

Winh = Window.innerheight;

} else if (document.documentelement && document.documentElement.clientHeight) {//IE 6 Strict Mode

WINW = Document.documentElement.clientWidth;

Winh = Document.documentElement.clientHeight;

} else if (document.body) {//other

WINW = Document.body.clientWidth;

Winh = Document.body.clientHeight;

}//For small pages with total size less then the viewport

var Pagew = (SCRW<WINW)? WINW:SCRW;

var Pageh = (Scrh<winh)? WINH:SCRH;

return {Pagew:pagew, Pageh:pageh, WINW:WINW, Winh:winh};

};

scroll bar position

function Getpagescroll () {

var x, y; if (window.pageyoffset) {//all except IE

y = Window.pageyoffset;

x = Window.pagexoffset;

} else if (document.documentelement && document.documentElement.scrollTop) {//IE 6 Strict

y = Document.documentElement.scrollTop;

x = Document.documentElement.scrollLeft;

} else if (document.body) {//All other IE

y = Document.body.scrollTop;

x = Document.body.scrollLeft;

}

return {x:x, y:y};

}

Jquery

Gets the height of the browser display area: $ (window). Height ();

Gets the width of the browser display area: $ (window). width ();

Gets the document height of the page: $ (document). Height ();

Gets the document width of the page: $ (documents). width ();

Gets the vertical height of the scroll bar to the top: $ (document). ScrollTop ();

Gets the vertical width of the scroll bar to the left: $ (document). ScrollLeft ();

The Calculate element position and offset method is a useful way to return the offset information for the first element in a wrapper set. By default, the offset information is relative to the body. The result contains top and left two properties. Offset (options, results) Options.relativeto Specifies the ancestor element relative to the offset position. This element should be relative or absolute positioned. Omitted is relative to body. Options.scroll whether the scroll bar is counted, by default true

Options.padding whether the padding is counted, default false options.margin whether margin is counted, default true options.border whether the border is counted, by default true

(6) At the end of the JavaScript code, the entire operation is done by calling the Finddimensions () function.

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

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.