Javascript Get Page Height With Scroll
3 November 06
| Permanent Link | Add Comments
I'm trying to find the most accurate cross browser/cross platform code imaginable to return the total height of a web page including the scroll. Below is what I came up with, has anyone seen anything better?
function getPageSizeWithScroll(){
if (window.innerHeight && window.scrollMaxY) {// Firefox
yWithScroll = window.innerHeight + window.scrollMaxY;
xWithScroll = window.innerWidth + window.scrollMaxX;
} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
yWithScroll = document.body.scrollHeight;
xWithScroll = document.body.scrollWidth;
} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
yWithScroll = document.body.offsetHeight;
xWithScroll = document.body.offsetWidth;
}
arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
return arrayPageSizeWithScroll;
}
Update:
Here is another twist on the code:
var Client = {
viewportWidth: function() {
return self.innerWidth || (document.documentElement.clientWidth || document.body.clientWidth);
},
viewportHeight: function() {
return self.innerHeight || (document.documentElement.clientHeight || document.body.clientHeight);
},
viewportSize: function() {
return { width: this.viewportWidth(), height: this.viewportHeight() };
}
};
If the mouse pointer changes to a hand when you roll-over an image associated with a story the image upon clicking either links to enlarged version of the image or a website associated with the image.
This is simpler and does the trick nicely:
var _docHeight = document.height || document.body.offsetHeight;
var _docWidth = document.width || document.body.offsetWidth;
I forgot the parentheses in my previous comment:
var _docHeight = (document.height || document.body.offsetHeight);
var _docWidth = (document.width || document.body.offsetWidth);
But that’s a “quick’n’dirty” way of doing it. Safer and better to use:
var _docHeight = (document.height !== undefined) ? document.height : document.body.offsetHeight;
var _docWidth = (document.width !== undefined) ? document.width : document.body.offsetWidth;
Fix for IE7 (at then end)
if( window.innerHeight && window.scrollMaxY ) // Firefox {
pageWidth = window.innerWidth + window.scrollMaxX;
pageHeight = window.innerHeight + window.scrollMaxY;
}
else if( document.body.scrollHeight > document.body.offsetHeight ) // all but Explorer Mac
{
pageWidth = document.body.scrollWidth;
pageHeight = document.body.scrollHeight;
}
else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
{ pageWidth = document.body.offsetWidth + document.body.offsetLeft; pageHeight = document.body.offsetHeight + document.body.offsetTop; }
A very nice script! Works in my FF, Opera and IE. Hope to see the day with standards …
function Viewport(){ this.windowX = (document.documentElement && document.documentElement.clientWidth) || window.innerWidth || self.innerWidth || document.body.clientWidth; this.windowY = (document.documentElement && document.documentElement.clientHeight) || window.innerHeight || self.innerHeight || document.body.clientHeight; this.scrollX = (document.documentElement && document.documentElement.scrollLeft) || window.pageXOffset || self.pageXOffset || document.body.scrollLeft; this.scrollY = (document.documentElement && document.documentElement.scrollTop) || window.pageYOffset || self.pageYOffset || document.body.scrollTop; this.pageX = (document.documentElement && document.documentElement.scrollWidth) ? document.documentElement.scrollWidth : (document.body.scrollWidth > document.body.offsetWidth) ? document.body.scrollWidth : document.body.offsetWidth; this.pageY = (document.documentElement && document.documentElement.scrollHeight) ? document.documentElement.scrollHeight : (document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight;
}
The comments box doesn’t allow much in the way of formatting, but this returns window size, scroll offset, and page size.
None of these seem to return the entire length including the portion below the visible area on a page in Firefox 2… anyone know why?
Nevermind… me being dumb, I was loading this inline in the header to test — the page needs to load BEFORE that value is > 0…
This is nice!
i really appreciate this code, thank u very much
well, you deleted my previous post (or I forgot to SUBMIT).. most likely you thaught is`a spam, but actualy the link destination was a page, where you can find a nice height, width etc. scripts, used in wordpress engine addons.
Pay attention on lightbox.js files functions getPageScroll() and getPageSize()
SORRY, the address:
http://www.huddletogether.com/projects/lightbox/
Oups !
http://elfz.laacz.lv/beautify/?
Hello,
I’am trying to make it work on FF and IE it’s look pretty with your code “extended” ;)
var client = { // Testé FF/IE getPage: function() { var pageWidth = 720; var pageHeight = 576; var scrollArr = this.getScroll(); var winArr = this.getWindow(); pageWidth = winArr.width + scrollArr.left; pageHeight = winArr.height + scrollArr.top; return { scrollX: scrollArr.left, scrollY: scrollArr.top, winW: winArr.width, winH: winArr.height, pageW: pageWidth, pageY: pageHeight }; }, getScroll: function() { return { left: this.scrollLeft(), top: this.scrollTop() }; }, getWindow: function() { return { width: this.windowWidth(), height: this.windowHeight() }; }, scrollLeft: function() { var xScroll = 0; if (self.pageXOffset) xScroll = self.pageXOffset; else if (document.documentElement && document.documentElement.scrollLeft) xScroll = document.documentElement.scrollLeft; else if (document.body) xScroll = document.body.scrollLeft; return xScroll; }, scrollTop: function() { var yScroll = 0; if (self.pageYOffset) yScroll = self.pageYOffset; else if (document.documentElement && document.documentElement.scrollTop) yScroll = document.documentElement.scrollTop; else if (document.body) yScroll = document.body.scrollTop; return yScroll; }, windowWidth: function() { var xWin = 720; if (self.innerHeight) xWin = self.innerWidth; else if (document.documentElement && document.documentElement.clientWidth) xWin = document.documentElement.clientWidth; else if (document.body) xWin = document.body.clientWidth; return xWin; }, windowHeight: function() { var yWin = 576; if (self.innerHeight) yWin = self.innerHeight; else if (document.documentElement && document.documentElement.clientHeight) yWin = document.documentElement.clientHeight; else if (document.body) yWin = document.body.clientHeight; return yWin; }
};
Thanks a bunch for this! I’ve been working on a CC project and this code made my job a whole lot easier.