Javascript

Get web page window size with Javascript

It’s difficult to know the exactly window size, because every browser has different way to render the pages.
The code below is compatible with a lot of browser, old version too.

Code

Inner / Offset / compatMode

document.body.offsetWidth: returns the innerwidth value from left hand side to the left side of the vertical scrollbar;
document.compatMode: Indicates whether the document is rendered;
window.innerWidth/innerHeight: widht/height of the browser window including, if rendered, the vertical scrollbar.

The use of multiple if is because every browser has different mode to render the page and different scrollbar size, with the code above are covered the most possible options.

 

Leave a Comment