B4J Question RunFunction with browser error

jkhazraji

Active Member
Licensed User
Longtime User
Hello everybody,
I want to run a Javascript function that I introduced by ws.Eval into the server, by clicking a button on the html page that I captured in the app as JQueryElement.
So I want the function to run in the click event of the button in server app as shown in the code section. However, it does not with an error in the console that states:
Uncaught TypeError: Cannot read properties of undefined (reading 'apply')
at b4j_runFunction (b4j_ws.js:51)
at WebSocket.b4j_ws.onmessage (b4j_ws.js:79)


The code:

B4X:
.
Private sub somefunc()
.
.

Dim code As String = $"
                 function hi(str){
               
                   alert(str);
                 }
                    "$
    ws.Eval(code,Null)
    .
    .
    .
End sub
Private Sub btn_Click (Params As Map)
   
    ws.RunFunction("hi", Array As String("Hello"))
    ws.Flush
   
End Sub

Any mastemind could expalin that.. I will be grateful
Regards
 

alwaysbusy

Expert
Licensed User
Longtime User
You seem to mix two things. You either use:

B4X:
Private Sub btn_Click (Params As Map)  
    Dim code As String = $"
                 function hi(str){             
                   alert(str);
                 }"$
    ws.Eval(code,Array As String("Hello"))
    ws.flush
End Sub

OR you put the javascript code of the hi function in your .html file between <script></script> tags and then use:

B4X:
Private Sub btn_Click (Params As Map)  
    ws.RunFunction("hi", Array As String("Hello"))
    ws.Flush  
End Sub

ws.eval does not add the javascript code to your html, it is 'temporary'.

Alwaysbusy
 
Upvote 0

jkhazraji

Active Member
Licensed User
Longtime User
You seem to mix two things. You either use:

B4X:
Private Sub btn_Click (Params As Map)
    Dim code As String = $"
                 function hi(str){           
                   alert(str);
                 }"$
    ws.Eval(code,Array As String("Hello"))
    ws.flush
End Sub

OR you put the javascript code of the hi function in your .html file between <script></script> tags and then use:

B4X:
Private Sub btn_Click (Params As Map)
    ws.RunFunction("hi", Array As String("Hello"))
    ws.Flush
End Sub

ws.eval does not add the javascript code to your html, it is 'temporary'.

Alwaysbusy
Hi,
I tried both of your methods but I got the same error😔
 
Last edited:
Upvote 0

jkhazraji

Active Member
Licensed User
Longtime User
Upvote 0

ilan

Expert
Licensed User
Longtime User
You seem to mix two things. You either use:

B4X:
Private Sub btn_Click (Params As Map)
    Dim code As String = $"
                 function hi(str){           
                   alert(str);
                 }"$
    ws.Eval(code,Array As String("Hello"))
    ws.flush
End Sub

OR you put the javascript code of the hi function in your .html file between <script></script> tags and then use:

B4X:
Private Sub btn_Click (Params As Map)
    ws.RunFunction("hi", Array As String("Hello"))
    ws.Flush
End Sub

ws.eval does not add the javascript code to your html, it is 'temporary'.

Alwaysbusy

if you define a function then you need to call it.

this should work.

B4X:
    Dim str As String = "Hello"
    Dim code As String = $"alert('${str}');"$
    ws.Eval(code,Null)
    ws.flush
 
Last edited:
Upvote 0
Top