Tuesday, March 15, 2011

Get document height (cross-browser)

I have a page built in 70% of html and javascript that does drag and drop images within a table. IE is not doing a good job at all on figuring out the content height, underneath is a great way to get the content height no matter what kind of browser it is. It always return the greatest value of the three, very useful.

Source: http://james.padolsey.com/javascript/get-document-height-cross-browser



This function will return any document’s height. It’s been tested in IE6/7, FF2/3, Safari (Windows), Google Chrome and Opera 9.5. If the actual document’s body height is less thanthe viewport height then it will return the viewport height instead:

The Code:

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)
);
}

Usage:

alert( getDocHeight() );

1 comments:

@Fei said...

Cross browser support is a pain. Try jquery. It can handle most of the problem for you :-)