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.
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 :)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With