Get document width and height, cross-browser

July 12th, 2012 by Alex Leave a reply »

Thanks to James Padolsey, who we all know well, here is a cross-browser method of getting a document width and height. I decided to save it here for future use:

function getDocumentDimensions() {
  var D = document;
  var h = Math.max(
    Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
    Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
    Math.max(D.body.clientHeight, D.documentElement.clientHeight)
  );
  var w = Math.max(
    Math.max(D.body.scrollWidth, D.documentElement.scrollWidth),
    Math.max(D.body.offsetWidth, D.documentElement.offsetWidth),
    Math.max(D.body.clientWidth, D.documentElement.clientWidth)
  );
  return { width: w, height: h };
}

Comments are closed.