Android Question execute the javascript on a page hosted by a external server

Jocelin

Member
Licensed User
Hello!

I want to execute a javascript function located on a web page hosted by a external server. By example, I have this code :

Sub panelDateSelected_Click

Dim JavascriptInterface1 As DefaultJavascriptInterface
dim patientFolder as WebView
dim WebViewExtras1 as WebViewExtras

patientFolder.Initialize("patientFloder")
patientFolder.url = "joctest.zapto.org" //is just for test.


WebViewExtras1.Initialize(patientFolder)
JavascriptInterface1.Initialize

WebViewExtras1.AddJavascriptInterface(JavascriptInterface1, "B4A")
Javascript="B4A.CallSub('Process_HTML', true, hello())" //The function call hello and display hello world

End sub


That not work, the page load but the function it not call.
I don'y know if I have the good code or if I use the good setup...

the code of the html page, that call when call url it :

<html>
<script>
function hello(){
alert("hello world");
}
</script>
<body style="text-align:center">
<div style="text-align:center;margin:20px;backgroud-color:#B1COD5">
<img src="monCadre2.jpg" alt="Kathleen" style="display:inline">
</div>
</body>
</html>



Thank you for your help.
 

Jocelin

Member
Licensed User
Hello, Thank you for your response. But I don't know the procedure for execute my javascript function on my server.
I found this code. I want to call the javascript function name hello, on the web site http://joctest.zapto.org

That not work.

Thank you for your help.


B4X:
'Activity module
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

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

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("layout1")
  
    '   add the B4A javascript interface to the WebView
    WebViewExtras1.addJavascriptInterface(WebView1, "B4A")
  
    '   now load a web page
    WebView1.LoadUrl("http://joctest.zapto.org") 'load url
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub WebView1_PageFinished (Url As String)
    '   Now that the web page has loaded we can get the page content as a String
  
    Dim Javascript As String
    Javascript="B4A.CallSub('ProcessHTML', true, hello)" 'try to call function hello. 
  

    Log("PageFinished: "&Javascript)
    WebViewExtras1.executeJavascript(WebView1, Javascript)
   
   
End Sub

Sub ProcessHTML(Html As String)
    '   This is the Sub that we'll get the web page to send it's HTML content to
  
    '   Log may truncate a large page so you'll not see all of the HTML in the log but the 'html' String should still contain all of the web page HTML
  
    Log("ProcessHTML: "&Html)
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Globals
   Private WebView1 As WebView
   Private ws As WebViewExtras
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   ws.addWebChromeClient(WebView1,"")
   WebView1.LoadUrl("http://joctest.zapto.org/")
End Sub

Sub WebView1_PageFinished (Url As String)
   ws.ExecuteJavascript(WebView1, "hello();")
End Sub
 
Upvote 0

Jocelin

Member
Licensed User
Hello Erel!

Thank you so much for your help. That work very well. I forgot the line
"ws.addWebChromeClient(WebView1,"")"o_O I never think to place it in my code.

Thank you, the problem it resolved. :)
 
Upvote 0
Top