JavaScript _javascript tips for capturing and judging browser windows, screens, Web pages, height, width, etc.

Source: Internet
Author: User
Tags numeric value

html Precise positioning attribute:scrollleft,scrollwidth,clientwidth,offsetwidth

ScrollHeight: Gets the scrolling height of the object.
ScrollLeft: Sets or gets the left distance between the left edge of an object and the current visible content in the window
ScrollTop: Sets or gets the distance between the top of the object and the topmost part of the visible content in the window
ScrollWidth: Gets the scrolling width of the object
Offsetheight: Gets the height of the object relative to the layout or parent coordinates specified by the parent coordinate offsetparent property
Offsetleft: Gets the calculated left position of the object relative to the layout or the parent coordinates specified by the Offsetparent property
offsettop: Gets the calculated top position of the object relative to the layout or the parent coordinates specified by the offsettop property
Event.clientx the horizontal coordinates of the relative document
Event.clienty vertical coordinates relative to document
The horizontal coordinates of the event.offsetx relative to the container
The vertical coordinates of the event.offsety relative to the container
Document.documentElement.scrollTop The vertical scrolling value
Event.clientx+document.documentelement.scrolltop relative to the document's horizontal coordinates + vertical scrolling amount
Ie,firefox differences are as follows:
IE6.0, ff1.06+:

Copy Code code 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


Hint: The margin attribute in CSS has nothing to do with ClientWidth, offsetwidth, ClientHeight, offsetheight

Page visible area wide: document.body.clientWidth
Page visible Area High: document.body.clientHeight
Web page Visible area wide: document.body.offsetWidth (including edge width)
Page visible Area High: document.body.offsetHeight (including edge height)
Page body Full text width: document.body.scrollWidth
The full text of the Web page High: document.body.scrollHeight
Web pages are rolled up high: Document.body.scrollTop
Pages were rolled away to the left: Document.body.scrollLeft
Web 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 Workspace height: window.screen.availHeight
Screen available workspace width: window.screen.availWidth


Technical points

This section of code mainly uses some of the properties of the document object with regard to the window, the main features and usage of these properties are as follows.
To get the size of the window, for different browsers, you need to use different properties and methods: To detect the real size of the window, you need to use the properties of Windows under Netscape; in IE, you need to go deep inside the document to detect the body; In a 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 the Window object contains the internal width of the current windows. The Innerheight property of the Window object contains the internal height of the current windows.
The Body property 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 document.
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.

Sample code

Copy Code code as follows:

<! Doctype>
<title>
Please adjust the browser window
</title>
<meta charset= "UTF8" >
<body>
&LT;H2 align= "center" >
Resize your browser window
<form action= "#" method= "Get" Name= "Form1" Id= "Form1" >
<!--display the actual dimensions of the browser window-->
The actual height of the browser window:
<input type= "text" name= "availheight" size= "4" >
<br>
The 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 ()//functions: Getting dimensions
{
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 detecting 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 function, get numeric value
Window.onresize = finddimensions;
-->
</script>
</body>

Source program Interpretation

(1) The program first establishes a form that contains two text boxes to display the current width and height of the window, and its value changes as the window size changes.
(2) in the following JavaScript code, the first defines two variables winwidth and winheight, which are used to save the height and width values of the window.
(3) Then, in function finddimensions (), 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) Further through the document inside the body to detect, 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.
(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.