Recommended:

  • phpclasses.org
  • jsclasses.org
  • jsmag.com
  • siteapps.com
  • View our reviews on Hot Scripts
  • JS Tutorial
  • scripts.com
  • securesignup.com




Recent Comments

Powered by Disqus




Back to articles

Get document dimensions (cross browser compatible)

Here are two javascript functions to get document dimensions, which works in all popular browsers. Basic idea is to check all possible values in all possible combinations and take maximal value, so if in one browser this value is undefined, it will be interpreted as 0 and could never be maximal value.

Here is a code snippet of those functions:
function getDocHeight() {
    var D = document;
    return 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)
    );
}
function getDocWidth() {
    var D = document;
    return 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)
    );
}

And here you can try it yourself:


You may also be interested in:

Powered by BlogAlike.com

blog comments powered by Disqus