B4J Question WebView add EventListener for DOM

xulihang

Active Member
Licensed User
Longtime User
I want to fire a B4J event when the text of some html element changes. I need to add a new EventListener but I don't know how to do this.

I found this code from the javafx doc:

Java:
EventListener listener = new EventListener() {
    public void handleEvent(Event ev) {
        Platform.exit();
    }
};

Document doc = webEngine.getDocument();
Element el = doc.getElementById("exit-app");
((EventTarget) el).addEventListener("click", listener, false);

Could someone convert this to B4J code?

I also found this thread: https://www.b4x.com/android/forum/t...king-a-button-inside-a-webview.98427/#content , but I don't know how to use this for elements other than button.
 

xulihang

Active Member
Licensed User
Longtime User
I found @stevel05 has made a wrapper which makes it easier to handle webengine.

As for adding event, if we use code from this thread, I figured this out to add event:

B4X:
Sub addClickEvent
    Dim we As JavaObject
    we = asJO(WebView1).RunMethod("getEngine",Null)
    Dim jsobject As JavaObject 'netscape.javascript.JSObject
    jsobject=we.RunMethod("executeScript",Array As String($"document.getElementById("p1")"$))
    Dim jscode As String=$"addEventListener("click", function()
{
   b4j.link('test');
});"$
    jsobject.RunMethod("eval",Array As String(jscode))
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…