JSinterface working once then no action

mc73

Well-Known Member
Licensed User
Longtime User
Hello, I've been working on setting an html page using webView from which I need to extract imageClick events and send the ID of image clicked to a b4a sub.
I've used JSInterface which returns the correct ID when I first run the app. Then, it returns nothing. Html is creation is done by a 'loadMap' routine and webView is added in design mode.

The code is:

B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
 Dim myInterface As JSInterface
 Dim WebView1 As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout ("setTablesNew")
myInterface.addJSInterface(WebView1, "B4A")
loadMap
End Sub

Sub loadMap

Dim myHtml As StringBuilder 
Dim myHtmlString As String 
Dim curTable As String 
Dim myImages(4) As String 

myImages(0)="table1.jpg"
myImages(1)="table2.jpg"
myImages(2)="table3.jpg"
myImages(3)="stand1.jpg"

myHtml.Initialize
myHtml.Append ("<html>")

myHtml.Append ("<script language='JavaScript'>")
myHtml.Append("function sendCellId(id){")
myHtml.Append("B4A.CallSub('b4aGetID', id);")
myHtml.Append ("}</script>")

myHtml.Append("<body><table border='2'>")

For k=1 To 20
myHtml.Append ("<tr>")
For l=1 To 20
curTable=(k-1)*20+l
myHtml.Append ("<td>")
Dim a As Int 
a=Rnd(0,10)
If a<4 Then
myHtml.Append ("<img id='" & curTable & "' onclick='javascript:sendCellId(id);' src='file://"& File.DirDefaultExternal & "/images/" & myImages(a) & "' alt='table' /><br>" & curTable)
End If
myHtml.Append ("</td>")
Next
myHtml.Append ("</tr>")
Next
myHtml.Append ("</table>")
myHtml.Append ("</body></html>")
myHtmlString=myHtml.ToString 
WebView1.Color=Colors.White 
WebView1.LoadHtml(myHtmlString)

End Sub

Sub b4aGetID(id As String )
Msgbox(id,"test")
End Sub

Any idea on what goes wrong and routine stops to send me data? Thank you.

SOLVED: I had to dim a new variable IN THE b4aGetId sub, instead of the original coming from the script, as follows:

Dim idnew As String
idnew=id
Msgbox(idnew,"test")

My new testing used webViewExtras as proposed by Erel.
 
Last edited:

mc73

Well-Known Member
Licensed User
Longtime User
Certainly. Though I've just noticed that javascript is not excecuted even if I set no call to jsInterface. Seems to be a problem of webView and I saw that others face the same, by googling for android's native browser. Ok, Erel, I will try the new lib and see.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
SOLVED: I had to dim a new variable in the b4aGetId sub, instead of the original coming from the script, as follows:

Dim idnew As String
idnew=id
Msgbox(idnew,"test")

My new testing used webViewExtras as proposed by Erel.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
I agree. You should be able to do it.

OK i have removed the JSInterface library attachment so it's no longer available for download.

And i have removed it from the Wiki pages.

Will it now get removed from the list of available libraries in the B4A HelpViewer or do i need to do something to get it removed from the HelpViewer?

Martin.
 
Upvote 0
Top