Android Question [SOLVED]Ultimatewebview2 javascript

elitevenkat

Active Member
Licensed User
Longtime User
How to use Javascript and get result?
If I call
Ultimatewebview2.evaluatejavascript("document.strung")
Where I can get the result?

Can someone guide me please ?
 
You can create a B4A event sub and have the javascript call it and pass the result:
B4X: Copy
    Javascript="B4A.CallSub('GetTitle_Event',  false, document.title)"
    wve.executeJavascript(wv, Javascript)  
   
Sub GetTitle_Event(Title As String)

    Log("Title=" & Title)
   
End Sub
 
Upvote 0
@JohnC
It does not work in ultimatewebview2.
' zwa is an instance of ultimatewebview2
i tried
Code: Copy
Private Sub zwv_PageFinished (Url As String) 'Works from API level 1 and above.
 
   Dim Javascript As String ="document.title"
    zwv.EvaluateJavascript(Javascript)

End Sub

Sub Evaluatejavascriptresult_event(res As String)
    Log("title " & res)    
End Sub
the event is not fired
can anyone help ?
 
Upvote 0
You removed critical code from my example like the "B4A.CallSub" part.

So, just try these lines exactly as shown and do NOT change anything to make sure you don't introduce a new problem:

B4X: Copy
Private Sub zwv_PageFinished (Url As String)
 
    Dim Javascript as string = "B4A.CallSub('GetTitle_Event',  false,  document.title)"
    zwv.EvaluateJavascript(Javascript)

End Sub

Sub GetTitle_Event(Title As String)

    Log("Title=" & Title)
 
End Sub
 
Upvote 0
You removed critical code from my example like the "B4A.CallSub" part.

So, just try these lines exactly as shown and do NOT change anything to make sure you don't introduce a new problem:

B4X: Copy
Private Sub zwv_PageFinished (Url As String)
 
    Dim Javascript as string = "B4A.CallSub('GetTitle_Event',  false,  document.title)"
    zwv.EvaluateJavascript(Javascript)

End Sub

Sub GetTitle_Event(Title As String)

    Log("Title=" & Title)
 
End Sub

i get the following in the log
message Uncaught ReferenceError: B4A is not defined is the error message at runtime
how to define b4a reference ?
 
Upvote 0
OK, so it seems the B4A.CallSub method/class is not available in UltimateWebView2.

This post seems to have solved this issue:
 
Last edited:
Upvote 0
So, maybe try this:

B4X: Copy
Sub Globals

    Private jsi As UltimateJavascriptInterface              

End Sub

Sub Activity_Create

    jsi.Initialize
    zwv.addJavascriptInterface(jsi, "B4X")

End Sub

Private Sub zwv_PageFinished (Url As String)
 
    Dim Javascript as string = "B4A.CallSub('GetTitle_Event',  false,  document.title)"
    zwv.EvaluateJavascript(Javascript)

End Sub

Sub GetTitle_Event(Title As String)

    Log("Title=" & Title)
 
End Sub
 
Upvote 0
So, maybe try this:

B4X: Copy
Sub Globals

    Private jsi As UltimateJavascriptInterface             

End Sub

Sub Activity_Create

    jsi.Initialize
    zwv.addJavascriptInterface(jsi, "B4X")

End Sub

Private Sub zwv_PageFinished (Url As String)
 
    Dim Javascript as string = "B4A.CallSub('GetTitle_Event',  false,  document.title)"
    zwv.EvaluateJavascript(Javascript)

End Sub

Sub GetTitle_Event(Title As String)

    Log("Title=" & Title)
 
End Sub

did that got following message in log
message Uncaught Error: Java exception was raised during method invocation
 
Upvote 0
this works

Code: Copy
Private Sub zwv_PageFinished (Url As String) 'Works from API level 1 and above.
    txtUrlZomato.Text=Url

    Dim Javascript As String ="document.title"
    zwv.EvaluateJavascript(Javascript)

End Sub

Sub zwv_evaluatejavascriptresult(res As String)
    Log("title " & res)
    
End Sub

thanks for your time.
 
Upvote 0
Back
Top