Android Question WebViewExtras1.ExecuteJavascript(js) not work! Why?

cenyu

Active Member
Licensed User
Longtime User
Hi friends, i have some trobles with WebViewExtras1 ver 2.20.
I have a web page login.html and jsfunction setHtml but when run this code:
B4X:
    html = File.ReadString(File.DirAssets , "login.html")
    html = html.Replace("__TC_APP_TITLE__","Time Controller")
       
    WebView1.Initialize("WebView1")
    Activity.AddView(WebView1, 0, 0, 100%x, 100%y)
    WebView1.JavaScriptEnabled = True
    WebView1.ZoomEnabled=False
    WebViewExtras1.Initialize(WebView1)
    'add the B4A javascript interface to the WebView
    WebViewExtras1.addJavascriptInterface(WebView1, "B4A")
    WebSettings.setDefaultTextEncodingName(WebView1,"utf-8")
   
   
    WebViewExtras1.LoadHtml(html)
     
   
    Dim js As String
    js = $"B4A.CallSub(setHTML('appVersion','1.0.0.2'))"$
    WebViewExtras1.ExecuteJavascript(js)

setHtml is not starting.
Where i am wrong?

My javascript function is simple:
JavaScript:
<script>
     function setHTML(elementId, val){
       alert(val);
       document.getElementById(elementId).innerHTML  = val;
    }

    </script>
 

gem67

New Member
hi
looks like theres minor syntax error when you call java script func:

Dim js As String
js = $"B4A.CallSub('setHTML', 'appVersion', '1.0.0.2')"$
WebViewExtras1.ExecuteJavascript(js)


this suppose that b4a 'setHtml' takes two parameters i.e parameterid and val. Pass this code
thanks
 
Upvote 0

cenyu

Active Member
Licensed User
Longtime User
May be:
B4X:
    Dim js As String
    js = $"setHTML('appVersion','1.0.0.2')"$
    WebViewExtras1.ExecuteJavascript(js)

setHTML is JS function into html. How to start setHTML from B4A code?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Check this example

edit: I tested the example works with WebViewExtra v1.42 but the JavaScript buttons click (Webview to B4A) does not work on WebViewExtras2 v2.20
 
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
you run your code within webview's PageFinished (Url As String) sub. you have to wait until the page loads.

B4X:
sub webview_pagefinished(url as string)
    Dim js As String
    js = $"setHTML('appVersion','1.0.0.2')"$
    WebViewExtras1.ExecuteJavascript(js)
end sub

B4A.CallSub() is for raising an event in b4a from the webpage. ExecuteJavascript() is the opposite
 
Last edited:
Upvote 0

FrostCodes

Active Member
Licensed User
Longtime User
? hmmmn I would update the example soon, I have one that works with the latest WebviewExtras. The change required is just a little different.
 
Upvote 0

FrostCodes

Active Member
Licensed User
Longtime User
I seldom use webview (+webbviewextras) but I want to buy you a coffee with the new update.
Thanks, I added the example for WebviewExtras2.
Check here for it.

Webview can be awesome if you learn to use it the right way, I love it as I can move my app to the web with little changes and still have it work for Android.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…