Android Question Getting javascript to run in webview

Please help me with getting javascript to run with webview, I added chrome client yet javascript never runs, only HTML runs

If you got an example project that runs javascript successfully please provide it

My code is as the following:

B4X:
Sub Globals
    Private w1 As WebView
    Private WVE As WebViewExtras
    Private DWCC As DefaultWebChromeClient
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")   
    Activity.Initialize("cl1")
    DWCC.Initialize("WebViewClient")
    WVE.Initialize(w1)
    WVE.SetWebChromeClient(DWCC)
    WVE.LoadUrl("https://..........")
End Sub

Thank you in advance
 

JohnC

Expert
Licensed User
Longtime User
Look at the second example code to see how Javascript is working in it:

 
Upvote 0
No need to do anything special for JavaScript to run in WebView.
I'm new here so thank you for offering B4X as a wonderful development software
And I'm looking into it, gone through many search results and still javascript didn't work

Look at the second example code to see how Javascript is working in it:

I followed the example, yet js doesn't work, here's my code:
B4X:
Sub Globals
    Private w1 As WebView
    Private WVE As WebViewExtras
    Private WebChromeClient1 As DefaultWebChromeClient
    Private JavascriptInterface1 As DefaultJavascriptInterface
End Sub

Sub Activity_Create(FirstTime As Boolean)

    JavascriptInterface1.Initialize
    WVE.Initialize(w1)
    WebChromeClient1.Initialize("DefaultWebChromeClient1")
    WVE.SetWebChromeClient(WebChromeClient1)
    WVE.addJavascriptInterface(JavascriptInterface1, "B4X")

    WVE.LoadUrl("https://.........")
    
End Sub

Thank you
 
Upvote 0
Look at the second example code to see how Javascript is working in it:

When I replace loadhtml with loadurl, in the example in the link above, and place my web page that has javascript, it displays correctly, means adding the following to the example
B4X:
WebView1.LoadUrl("https://........")

Not sure why my code doesn't display the web page correctly
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
OK, I am confused...

Your example code has been using the ".LoadURL" method. Then your latest above post says "When I replace loadhtml with loadurl" which suggests you were actually using .loadhtml and not the .loadurl in your code - is this correct?

So are you saying that your code works when in an external file and using the .loadurl, but does NOT work when loading the same HTML code using .loadhtml?

If so, does the HTML code you are using with .LoadHTML contain any external HTTP references (maybe to external javascript code files)?

If so, then ChatGPT says:

External JavaScript files: If the HTML string contains <script src="..."> tags pointing to external JavaScript files, the LoadHtml method will not automatically fetch and execute those external files. This is because the LoadHtml method is designed for loading static HTML and doesn't have a URL context that the WebView can use to resolve external resources (like scripts, stylesheets, or images).
 
Upvote 0
OK, I am confused...

Your example code has been using the ".LoadURL" method. Then your latest above post says "When I replace loadhtml with loadurl" which suggests you were actually using .loadhtml and not the .loadurl in your code - is this correct?

So are you saying that your code works when in an external file and using the .loadurl, but does NOT work when loading the same HTML code using .loadhtml?
I mean replacing with the loadurl of which I use originally, I replaced it in the example in the link you've provided. Means I removed loadhtml (which is just in the example, in my code I use loadurl), with loadurl and the page displays in full including javascript

So that means the example you provided works correctly, but I'm still unable to implement it

Thank you
 
Upvote 0
When I add the following it works (javascript web page loads):
B4X:
Dim jo As JavaObject = w1
    jo.RunMethod("setLayerType", Array(2, Null))
    WebViewSetting1.setDatabaseEnabled(w1, True)
    WebViewSetting1.setDOMStorageEnabled(w1, True)
    WebViewSetting1.setGeolocationEnabled(w1, True)
    
    WebViewSetting1.setSaveFormData(w1, False)
    WebViewSetting1.setSavePassword(w1, False)
    WebViewSetting1.setJavaScriptCanOpenWindowsAutomatically(w1, False)
    
    WebViewSetting1.setSupportZoom(w1, False)
    WebViewSetting1.setUseWideViewPort(w1, False)
    WebViewSetting1.setMediaPlaybackRequiresUserGesture(w1, False)
    
    WebViewSetting1.setDatabasePath(w1, "")

It seems fine for now, and thank you very much for helping with it
 
Upvote 0

aminoacid

Active Member
Licensed User
Longtime User
Does your JavaScript run in a regular (desktop) Browser? Make sure that it does first. I have use webview with some pretty complex JavaScript and it works very well in webview.
 
Upvote 0
Top