Android Question WebviewExtras and Javascript variables

jcmartini

Member
Licensed User
Longtime User
I have the following definitions:

Dim WVExtras As WebViewExtras
...
1) WVExtras.addJavascriptInterface(WebView1, "B4A")
2) WVExtras.addWebChromeClient(WebView1, True)

Execution:

3) js = "B4A.CallSub('processHTML', document.getElementById(""" & RankCol & """).style.color='red')"
Or:
4) js = "B4A.CallSub('processHTML', var elem=document.getElementById(""" & RankCol & """).value;elem.style.color='red')"
5) WVExtras.executeJavascript(WebView1, js)

With 2) and with 4), 5) doesn't work and I receive the following error msg: "Uncaught SyntaxError: Unexpected token var in file:/// (Line: 1)".
Without 2) and with 4), 5) doesn't work and I receive no error msg.
With 3) works perfecly without 2) and with 2).

How should I proceed for defining Javascript variables ?
 

Brandsum

Well-Known Member
Licensed User
Why are you calling B4A.Callsub every time? That is only needed if you want to return some value from webpage to the activity.
B4X:
'3)
js = "document.getElementById('" & RankCol & "').style.color='red';)"
'Or:
'4)
js = "var elem=document.getElementById('" & RankCol & "'); elem.style.color='red';)"
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
webviewextras.executeJavacript() goes from Activity to webpage. use it to set your variables.
B4A.CallSub() allows webpage to send a string back to Activity, if needed.

if i infer from your comments correctly, you're looking to set or modify variables dynamically. executeJavacscript() applies.
if you're just looking to set some variables in a webpage, you don't need either of the functions mentioned above. just create your webpage (either as a string or saved to a file) and use webview's loadHTML() to display the page in a webview. (you could, optionally, use executeJavascript() later, if you wanted.)
 
Upvote 0
Top