0

I'm curious how I would get the width of the document, it looks like I can get the width of the window using document.body.offsetWidth but everything else I try is like a few pixels different from document.body.offsetWidth and I need the document's width which is larger, and doesn't change when the window is resized.

erikvold
  • 15,988
  • 11
  • 54
  • 98

1 Answers1

4
function getSize()
{
    var w=window,d=document,e=d.documentElement,g=d.getElementsByTagName('body')[0];
    var x=w.innerWidth||e.clientWidth||g.clientWidth;
    var y=w.innerHeight||e.clientHeight||g.clientHeight;
    return {width:x, height:y}
}
var size=getSize();
console.log(size.width+" "+size.height);

DEMO.

The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • this returns the size of the window, that is why the values change when I change the size of the result frame, I need the value that does no change when there is an image taking up 2000x3000 pixels (for example). – erikvold Apr 27 '12 at 23:52
  • http://www.howtocreate.co.uk/tutorials/javascript/browserwindow and http://stackoverflow.com/questions/5484578/how-to-get-document-height-and-width-without-using-jquery – The Alpha Apr 28 '12 at 00:03
  • 1
    +1 for vanilla JavaScript, whether the OP asked for it or jQuery; we often forget how easy libraries like jQuery make JavaScript. – Bojangles Apr 28 '12 at 00:54