I have the following HTML document:
<html>
<head>
</head>
<body>
<div>
<p>blah blah blah</p>
</div>
</body>
</html>
I need to determine the height of <div></div> to adjust my WebView height accordingly, which I do the following way:
document.getElementsByTagName('div')[0].scrollHeight
Sometimes the value returned is correct, sometimes it is a bit less than actual. I call this piece of JavaScript in WebViewClient.onPageFinished() method, so the page should have been rendered by this time.
I also tried .clientHeight, .offsetHeight, and even .getBoundingClientRect().height
Is there any way to get correct and consistent values?
To get the exact value of a style attribute for any element, you can use this
// get the reference to the element
var myDiv = document.getElementsByTagName('div')[0];
// get the desired height attribute
var computedHeight = document.defaultView.getComputedStyle( myDiv, null ).getPropertyValue( 'height' );
Hope that helps.
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