Get window/viewport/document height and width [JavaScript]

Via andylangton and james.padolsey we can find the viewport and document height/width in JavaScript:

function getPageSize() {
  var vw = {width:0, height:0};
  var doc = {width:0, height:0};
  var w=window, d=document, dde=d.documentElement, db=d.getElementsByTagName('body')[0];
  
  // viewport size
  vw.width  = w.innerWidth||dde.clientWidth||db.clientWidth;
  vw.height = w.innerHeight||dde.clientHeight||db.clientHeight;

  // document size
  doc.width  = Math.max(db.scrollWidth, dde.scrollWidth, db.offsetWidth, dde.offsetWidth, db.clientWidth, dde.clientWidth);
  doc.height = Math.max(db.scrollHeight, dde.scrollHeight, db.offsetHeight, dde.offsetHeight, db.clientHeight, dde.clientHeight);
  
  // if IE8 there is a bug with 4px
  if (!!(document.all && document.querySelector && !document.addEventListener) && (vw.width+4 == doc.width) && (vw.height+4 == doc.height)) {
    vw.width=doc.width;
    vw.height=doc.height;
  }
  
   return {vw:vw, doc:doc};
}

getPageSize(); // -> {vw: { width:xxx, height:xxx }, doc: { width:xxx, height:xxx } }

It should work for IE8-IE10 and all modern browsers.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

*