Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery returns wrong document height

I'm using a Webview in Cocoa (Mac), and I'm trying to get the correct height of a document. Just doing the normal thing, webview.mainFrame.frameView.documentView.bounds.size.height seems to work fine in all cases, except one that I just found:

http://www.medium.com.

When I try doing that on this site, I get a document height of something like 500, when the real document height is more like 2000+, since I have to scroll several screens to get to the end.

I tried resorting to evaluating javascript in the webview to get the actual height, using approaches like the ones listed here: How to get height of entire document with JavaScript?

But these also gave grossly incorrect values, like my initial approach. Even going to that page, loading jQuery, then doing stuff like $(document.body).height() gives a grossly incorrect value. Anyone know what is going on? Is there some other way of getting the document height that is reliable?

I noticed that the heights reported by the inspector in chrome are getting confused also. In these screenshots, you can see the document.body height being reported as less than the height of one of its child nodes.

medium.com body height in chrome inspectormedium.com body->child div height in chrome inspector

like image 696
Isak Avatar asked Jan 15 '14 08:01

Isak


2 Answers

The thing with this page is the following:

The HTML and Body (and a few of the underlying elements) are all set to a 100% height, so they fit exactly in the browser-window. In my case that is about 500pixels high. How do they establish this scrolling then? Well... There is a div with a overflow:auto and the actual content is way more (around 4000px)

So you are in fact not scrolling the whole page, but this specific div (check out layout-foreground scrolling-region)

In this case it is totally correct that the height of the document is 500px, the height you are looking for can be found this way via the console:

document.querySelectorAll(".scrolling-region")[0].scrollHeight

That will give you the height of 4133px, which is, in this case, the actual height of the page.

Cheers :)

like image 111
Jaco Koster Avatar answered Oct 17 '22 00:10

Jaco Koster


Looks like html,body{height:100%} and .screen-scroll,.screen-scroll>body{overflow:hidden} are messing things up for you. If you set them both to height:100% and set body to overflow:hidden then html and body will both have the same 100% height with no overflow, which will make the document height the same as the window height.

So it may be that you're actually looking for the sum of the heights of header, nav, and .background-white instead, which would be more like 4222 and not like the 500 you are getting now.

something like this:

var hh = $('header').height(),
    nh = $('nav').height(),
    bh = $('.background-white').height(),
    docHeight = hh+nh+bh;
alert(docHeight);//test
like image 42
FiLeVeR10 Avatar answered Oct 17 '22 01:10

FiLeVeR10