views:

1231

answers:

3

I want to provide my visitors the ability to see images in high quality, is there any way I can detect the window size (or better yet, the viewport size - see green area) of the browser with Javascript?

+5  A: 

jquery ? $(window).width() and $(window).height()

Scott Evernden
Thank you all for the quick answers.
Alix Axel
a non jQuery solution would be good
allyourcode
+2  A: 

If you aren't using jQuery, it gets ugly. Here's a snippet that should work on all new browsers. The behavior is different in Quirks mode and standards mode in IE. This takes care of it.

var elem = (document.compatMode === "CSS1Compat") ? 
    document.documentElement :
    document.body;

var height = elem.clientHeight;
var width = elem.clientWidth;
Chetan Sastry
Doesn't this give you the height of the page, not the viewport? That's what this page seems to indicate: https://developer.mozilla.org/en/DOM/element.clientHeight
allyourcode
+1  A: 
CMS
Doesn't work in IE - http://www.quirksmode.org/dom/w3c_cssom.html#windowview
Chetan Sastry
Even though this doesn't work in IE +1 for diagram :D. For a question like this, it should be a crime not to have more of these.
allyourcode