Android Question javascript and stringbuilder (how to pass b4X variable?)

GERSON PINTO

Member
Licensed User
Hello!
I am trying to pass the content of the variable "x" to use in a javascript with stringbuilder.

Code:
Dim x As String
    x="some text to put inside"
    javascript2.Initialize
    javascript2.Append("var toInsert = document.createElement('div');")
    javascript2=javascript2.Append("toInsert.innerHTML =`x`;")
    WebView1.ExecuteJavascript(javascript2)

I need to pass the content of variable "x"
How I can do? Any help?
 

drgottjr

Expert
Licensed User
Longtime User
B4X:
'you need webviewextras to execute javascript, not webview
dim wvx as webviewextras
dim javascript2 as stringbuilder
javascript2.initialize
javascript2.append("var toInsert = document.createElement('div');").append("toInsert.innerHTML = '").append(x).append('";")
log( "looks like this: " & javascript2.tostring )
wvx.execute.javascript( webview1, javascript2.toString )

' pay close attention to single quotes next to double quotes
' no guarantee that code will actually do what you think it does.  you asked only how to include variable x in the stringbuilder.
' ---------------------------------------------------------------------------------------------------------------------------------------------
' you can also do this with a smart string and have it look just like html text:

dim smartjavascript as string = $"var toInsert = document.createElement('div');
toInsert.innerHTML = '${x}';
"$

Log("done with smart string: " & smartjavascript)
 
Last edited:
Upvote 0

GERSON PINTO

Member
Licensed User
Great!
Works fine!
Thank you very much
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…