How to get the size of the browser’s window in javascript
Several days ago, I was looking for a way to get the size of the browser’s window in javascript. Usually, when I need something, I google first, because I don’t want to “re-implement the wheel”. You can find lots of good scripts on the web, just waiting to be used. But, unfortunately, for what I needed, I couldn’t find any good candidates. So, I’ve wrote one myself, and post it here, in case anyone else might be interested.
There were 3 things that a suitable script for my problem needed to return:
- the viewport’s height a.k.a. the height of the visible area in the browser’s window
- the total height of the page
- how much did the user scrolled
And this small javascript does it all. It even works (partially) in Internet Explorer. But I will post a fix for that soon enough. It works now. Tested in Firefox, Opera, Konqueror and Internet Explorer.
var x = window.getSize();
alert( "The viewport's height is " + x.height + "px out of a total of " + x.totalHeight + "px. You've scrolled " + x.vScroll + "px" );
Test it!. It’s the same on the X axis. But always keep in mind that horizontal scrollbars are lame.
Download
This script is available for download here. The source code is commented, so you can find more information about how it works there.

