Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WebView: Element height returned by JavaScript is inconsistent

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?

like image 639
Andrii Chernenko Avatar asked Mar 15 '26 23:03

Andrii Chernenko


1 Answers

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.

like image 190
SHANK Avatar answered Mar 18 '26 13:03

SHANK