JavaScript to get the visible window size of various browsers

Source: Internet
Author: User
Visible area width: document. body. clientwidth: the visible area of the webpage is high: document. body. visible area width of clientheight webpage: document. body. offsetwidth (including the width of the edge) visible area of the webpage Height: document. body. offsetheight (including the height of edges): Document. body. scrollwidth: The full text of the webpage is high: document. body. scrollheight the height of the webpage to be rolled: document. body. the left: document. body. scrollleft: window. screentop page text part left: window. screenleft screen resolution Height: window. screen. height the screen resolution width: window. screen. width Screen Workspace Height: window. screen. availheight available workspace width: window. screen. availwidth visible area width of the webpage: document. body. clientwidth: the visible area of the webpage is high: document. body. visible area width of clientheight webpage: document. body. offsetwidth (including the width of the edge) visible area of the webpage Height: document. body. offsetheight (including the height of edges): Document. body. scrollwidth: The full text of the webpage is high: document. body. scrollheight the height of the webpage to be rolled: document. body. the left: document. body. scrollleft: window. screentop page text part left: window. s The height of the creenleft screen resolution: window. screen. height the screen resolution width: window. screen. width: available workspace height of the screen: window. screen. availheight available workspace width: window. screen. availwidth visible area width of the webpage: document. body. clientwidth: the visible area of the webpage is high: document. body. visible area width of clientheight webpage: document. body. offsetwidth (including the width of the edge) visible area of the webpage Height: document. body. offsetheight (including the height of edges): Document. body. scrollwidth: The full text of the webpage is high: document. body. scrollheight the height of the webpage to be rolled: document. body. scrolltop web page rolled out Left: document. body. scrollleft: window. screentop page text part left: window. screenleft screen resolution Height: window. screen. height the screen resolution width: window. screen. width: available workspace height of the screen: window. screen. availheight available workspace width: window. screen. availwidthhtml: scrollleft, scrollwidth, clientwidth, and offsetwidth: get the scroll height of an object. Scrollleft: sets or obtains the scrolltop distance between the left edge of the object and the leftmost end of the currently visible content in the window: set or get the scrollwidth between the top of the object and the top of the visible content in the window: get the scroll width of the object offsetheight: obtain the height of an object relative to the layout or the height of the parent coordinate specified by the parent coordinate offsetparent attribute offsetleft: Obtain the left position of the object relative to the layout or the parent coordinate specified by the offsetparent attribute offsettop: obtains the top position event of an object relative to the layout or the parent coordinate specified by the offsettop attribute. the horizontal coordinate of clientx relative to the document(Excluding X of the scroll bar) The vertical coordinates of event. clienty relative to the document (Excluding the y of the scroll bar) Event. offsetx relative to the horizontal coordinate of the container event. offsety relative to the vertical coordinate of the container document.doc umentelement. scrolltop vertical scroll value event.clientx?document.doc umentelement. scrolltop Relative document horizontal coordinate + vertical scroll volume
Event. clientx/event. clienty: when an event occurs, the cursor is relative to the position in the upper left corner of the visible document area in the browser window;
(In the DOM standard, the two attribute values do not consider the scrolling of the document, that is, no matter where the document is rolled, as long as the event occurs in the upper left corner of the window,
Both clientx and clienty are 0, Therefore, in IE, you need to get the coordinates of the event relative to the position at the beginning of the document,
Add document. Body. scrollleft and document. Body. scrolltop ). Even. Keycode: return the keydown key when the keyup event occurs.CodeAnd Unicode characters of the keypress event;
(Firefox2 does not support event. keycode, which can be replaced by event. Which)  

 

Example

 

 
IE6/7/8: document can be used for pages without doctype declaration. body. scrolltop to get the scrolltop height. For pages with doctype declarations, you can use document.doc umentelement. scrolltop; Safari: Safari is special. You can obtain the scrolltop function by yourself: window. pageyoffset; Firefox: Firefox and other relatively standard browsers are much more worry-free, directly using document.doc umentelement. scrolltop; var scrolltop = document.doc umentelement. scrolltop | window. pageyoffset | document. body. scrolltop;

 W3C standard (if this line is added to the page) 
   in IE: document. body. clientwidth ==> body object width document. body. clientheight => bodyobject height document.doc umentelement. clientwidth ==> wide distance document.doc umentelement. clientheight => the height of the visible area is in Firefox: document. body. clientwidth ==> body object width document. body. clientheight => bodyobject height document.doc umentelement. clientwidth ==> wide distance document.doc umentelement. clientheight => the height of the visible area is in Opera: document. body. clientwidth => visible area width document. body. clientheight ==> zookeeper High Level document.doc umentelement. clientwidth ==> Page Object width (that is, the bodyobject width is added to the marginwidth extension document.doc umentelement. clientheight ==> Page Object height (that is, the height of the body object plus the margin height) 
If W3C standards are not defined, ie is: document.doc umentelement. clientwidth => 0document.doc umentelement. clientheight => 0firefox: document.doc umentelement. clientwidth ==> Page Object width (that is, the bodyobject width is added to the marginwidth extension document.doc umentelement. clientheight ==> Page Object height (that is, the height of the body object plus the margin height) Opera: document.doc umentelement. clientwidth ==> Page Object width (that is, the bodyobject width is added to the marginwidth extension document.doc umentelement. clientheight => Page Object height (that is, the height of the body object plus the margin height)

 

Determine the browser type: var UA = navigator. useragent. tolowercase (); var OS = new object (); OS. isfirefox = UA. indexof ("gecko ")! =-1; OS. isopera = UA. indexof ("Opera ")! =-1; OS. isie =! OS. isopera & UA. indexof ("MSIE ")! =-1;

 

 

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.