At times you may want to get the size of the window. I don’t remember where I came across the original code, but I modified it and created the following function:


function getWindowSize() {
  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
  };
}

As far as I can tell, this should work in all browsers, but if you find that it doesn’t at times, please let me know. šŸ˜‰

Categories: BlogJavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *