Get browser window size
The following content is taken from JavaScript-related books and networks. I feel that the current situation can be simplified.
(1) to obtain the window size, different properties and methods must be used 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, to get the size of the window, pay attention to the size of the root element, instead of the <body> element.
(2) 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.
(3) 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.
(4) 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. (For IE6)
<! 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>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> available region checker </title>
</Head>
adjust the browser window size
height of the browser window:
width of the browser window:
/* Nasty hack to deal with doctype swith in IE */
// 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;
Winwidthappsdocument.doc umentelement. clientwidth;
}
// Output the result to two text boxes
Document. form1.availheight. value = winheight;
Document. form1.availwidth. value = winwidth;
}
Finddimensions ();
Window. onresize = finddimensions;
// -->
</SCRIPT>
</Body>
</Html>
SourceProgramExplanation
(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 the subsequent JavascriptCodeFirst, two variables winwidth and winheight are defined to save the window height and width values.
(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.