views:

30

answers:

1

Hi,

I need to find out height of a page including the content that goes beyond the vertical scrolls which is hidden. I have searched google and stackoverflow QA's but those solution don't apply to my situation.

One condition is I cannot change HTML or BODY css. Basically I am trying this out for a firefox extension

All the methods like

offsetHeight, scrollHeight, getComputedValue

didn't work somehow on a website noupe.com. Everytime I tried finding the body's above mentioned property it just gave me 18px even though the page height is more than 4000 px.

Tried jQuery's

$(document).height();

Didn't work too.

This is how I am finding out page height:

var x = window.content;
var pageBody = x.document.getElementsByTagName("body")[0];    
var my_pageheight = parseInt(x.document.defaultView.getComputedStyle(pageBody, "").getPropertyValue("height"));

This gives me 18px on noupe.com but WORKS WELL on other websites. Any clue how i can make this work.

Thanks

+1  A: 

How about

$('body').height();

I was going to post an explanation, but Alex in the comments below did it for me, so instead I suppose I'll just post some links to places where you can find more information on this:

http://dev.opera.com/articles/view/35-floats-and-clearing/
http://www.smashingmagazine.com/2007/05/01/css-float-theory-things-you-should-know/

Also remember that since we're dealing with the body element we can't use the overflow: hidden method (which would remove the scrollbars from the page).

Also, thank goodness you can inject in new elements, I was thinking of using this as a solution...

var max = 0;

$('body *').each(function(){
    var current = $(this);
    if ((current.offset().top + current.height()) > max) max = current.offset().top + current.height();
});

Probably counts as one of the worst pieces of Javascript I've ever written, since you stated that you can't add anymore CSS or HTML. The following, (assuming you still need it) should work:

$('<div />').css({
    'height': 0,
    'width': 0,
    'visibility': 'hidden',
    'clear': 'both'
}).appendTo('body');

$('body').height();
Yi Jiang
it just gives me 18px. :(even I have a script that finds out the height but it just fails on noupe.com
offhell
@offhell the reason why it fails there is because of floats. If you view it in Firebug you can see that the `body` **is** 18px high. To "fix" this, you'd need to clear the float somehow, and the easiest method is to inject a clearfix element after the last element, but that's out of the question here
Yi Jiang
offhell
Well your solution definately works in my situation as I am building a firefox addon and I am very much allowed to insert elements.
offhell
Floats technically remove an element from the natural 'flow' of the document. When you float two elements side by side they 'break' out of their container, which means the container does not 'container them and will therefore have a height of whatever else is in there (padding, other elements etc). Clearing your floats after the container allows the container element to 'contain' it's floated elements properly http://webdesign.about.com/od/advancedcss/a/aa010107.htm
Alex