B4J Question How to get the height of a DIV in webview? [SOLVED]

jmon

Well-Known Member
Licensed User
Longtime User
Hello,

So as the title says, I want to get the height of a DIV in a webview. I tried things like this:
B4X:
    Private we, temp As JavaObject  
    Private js As jScriptEngine

    we.InitializeNewInstance("javafx.scene.web.WebEngine",Null)
    temp = wvMessage
    we = temp.RunMethod("getEngine",Null)

    Dim s As StringBuilder
    s.Initialize
    s.Append("<!DOCTYPE html>")
    s.Append("<html>")
    s.Append("<head>")
    s.Append("<style Type='text/css'>")
    s.Append("a:link {color:#3D8FAB;} ")
    s.Append("a:visited {color:#2b6a7f;}")
    s.Append("a:hover {color:#66cbed;}")
    s.Append("a:active {color:#ffffff;}")
    s.Append("</style>")
    s.Append("</head>")
    s.Append("<body bgcolor=#000000>")
    s.Append("<div id='message' style='color:" & sColor & "; font-size:0.8em; font-family:arial;'>")
    s.Append(sMessage.Replace(CRLF, "<br>").Replace(TAB, "&nbsp&nbsp&nbsp&nbsp"))
    s.Append("</div>")
    s.Append("</body>")
    s.Append("</html>")

    wvMessage.LoadHtml(s)

    Dim Height As Object
    js.enginePut("doc",we.RunMethod("getDocument",Null))
    js.evalString("var height = doc.getElementById('message').clientHeight;") ' get the height of the div
    Height = js.engineGet("height")

But it will only return:
B4X:
java.lang.NumberFormatException: For input string: "sun.org.mozilla.javascript.internal.Undefined@28fbb9"

Thanks in advance for your help.

[EDIT] Solved in post #3 Thanks to warwound.
 
Last edited:

warwound

Expert
Licensed User
Longtime User
Has the WebView not fully rendered the HTML when your javascript is executed?
Once the WebView has rendered the HTML your javascript may work but until it is rendered will not...

Martin.
 
Upvote 0

jmon

Well-Known Member
Licensed User
Longtime User
Has the WebView not fully rendered the HTML when your javascript is executed?
Once the WebView has rendered the HTML your javascript may work but until it is rendered will not...

Martin.
Thanks warwound,

Your answer helped me find my mistake. I had to put the code in the PageFinished event for it to work.
B4X:
Private Sub wvMessage_PageFinished (Url As String)
    Dim Height As Object
    js.enginePut("doc",we.RunMethod("getDocument",Null))
    js.evalString("var height = doc.getElementById('message').clientHeight;") 'get the height of the div
    Height = js.engineGet("height")
    If IsNumber(Height) Then wvMessage.PrefHeight = Height + 20
End Sub

Thanks a lot!

[edit] fixed a mistake in the code: replaced doc.getElementById('parent') with doc.getElementById('message').
 
Last edited:
Upvote 0
Top