Design & Download
Your JS Library
in Seconds!!!

Snippet: getViewportSize() - Viewport Dimensions

Written by Christopher West (cwest) on December 24, 2015.
Gets the dimensions of the viewport or in other words the dimensions of the window that is visible.
function getViewportSize() {
  var doc = document,
    body = doc.body,
    docElem = doc.documentElement,
    docElemHeight = docElem.clientHeight,
    docElemWidth = docElem.clientWidth,
    css1Compat = doc.compatMode === 'CSS1Compat';
  return {
    height: css1Compat && docElemHeight || body && body.clientHeight || docElemHeight,
    width: css1Compat && docElemWidth || body && body.clientWidth || docElemWidth
  };
}