Android Question WebViewExtras1.JavaScriptEnabled not work

cenyu

Active Member
Licensed User
Longtime User
I using WebViewExtras ver. 2.20
I am trying to call B4A sub from my web site
This below is my code but cant start b4a sub from html page loaded into WebView
B4X:
Sub Globals
    Dim WebView1 As WebView
    Dim WebViewExtras1 As WebViewExtras
  
    Dim html As String

End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("111")
'    WebView1.Initialize("")
  

    WebViewExtras1.Initialize(WebView1)
    '   WebViewExtras1 now has all the methods and properties of WebView1 plus it's additonal methods and properties
    '   so you can use WebView1 to get/set WebView properties/methods
    '   or use WebViewExtras1 to get/set WebView1 properties/methods with the additional properties/method of WebViewExtras

    Dim WebViewClient1 As DefaultWebViewClient
    WebViewClient1.Initialize("WebViewClient1")
    WebViewExtras1.JavaScriptEnabled=True
    WebViewExtras1.SetWebViewClient(WebViewClient1)
    WebViewExtras1.AddJavascriptInterface(WebViewClient1,"B4A")
    WebViewExtras1.CanGoback()
  
    WebViewExtras1.LoadUrl("https://192.168.0.106/MobileOffice/D2548BA4-E6FD-43A0-9423-F4AE0BC7B710/Phone/Auth/Authorize/" & pId.GetDeviceId)
    
End Sub


Sub WebViewGoBack_Request()
    WebViewExtras1.GoBackOrForward(-1)
    Log(9)
End Sub


And this is my html code:
HTML:
<div class="left">
        <a href="javascript:;" onclick="goBack()" class="headerButton"> <!--goBack-->
            <ion-icon name="chevron-back-outline"></ion-icon>
        </a>

</div>

<script>
    function goBack() {
      
        B4A.CallSubPlus('WebViewGoBack_Request', true);
    }
</script>

Is this correct way to call b4a sub from html page?
 
Last edited:

JohnC

Expert
Licensed User
Longtime User
You need a sub in your B4A code that is named "WebViewGoBack_Request" that will do the WebViewClient1.Back function in it:
B4X:
Sub WebViewGoBack_Request()    'you need this sub in B4A so the webpage can call this sub
    WebviewClient1.Back
End Sub
 
Upvote 0

cenyu

Active Member
Licensed User
Longtime User
I have it...But my html code does not reach the b4a sub
I think may be WebViewExtras1.JavaScriptEnabled=True is not enough?
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Well, I would narrow the issue down by replacing the B4a.CallSubPlus with a known working "alert" call:

B4X:
<script>
    function goBack() {
      
        alert("This is a test");
    }
</script>

This will narrow down if the problem is that the goBack is not being called, or if the B4A.CallSub is not working right, so then we will know were to focus our troubleshooting efforts.

So, does the Alert pop-up display when you click the hyperlink using the above modification?
 
Upvote 0

cenyu

Active Member
Licensed User
Longtime User
I put
alert("This is a test");

Nothing.... the Alert pop-up display not shown
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Try adding "void(0) in this line:
B4X:
<a href="javascript:void(0);" onclick="goBack()" class="headerButton"> <!--goBack-->
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Also, I think your code to enable javascript should be:

B4X:
WebView1.JavaScriptEnabled = True

'instead of

WebViewExtras1.JavaScriptEnabled=True
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
You can post the ZIP in this thread so others can help too.
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Hi:

Maybe not the same thing, but I’ve just work with webview one time long ago.
Maybe you can try this example and see if it works for you, and what you need is similar

 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I can send you my project to test...If you have time
Before uploading your ZIP file, please modify your sample project to load a local HTML page into webview (since we will not have access to your private 192.xxx server)
 
Upvote 0

cenyu

Active Member
Licensed User
Longtime User
This is my project...I can't call B4A sub from html

Thanks for your efforts!
 

Attachments

  • WebViewExtrasJava.zip
    60.5 KB · Views: 362
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I think we first need to figure out why a simple javascript alert call is not working and that might then fix everything.
 
Upvote 0

cenyu

Active Member
Licensed User
Longtime User
The solution is into this code from José J. Aguilar


B4X:
    Dim JavascriptInterface1 As DefaultJavascriptInterface
    JavascriptInterface1.Initialize

Now full working code is:
B4X:
Sub Globals
    Dim WebView1 As WebView
    Dim WebViewExtras1 As WebViewExtras

    Dim html As String

End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("111")
'    WebView1.Initialize("")


    WebViewExtras1.Initialize(WebView1)
    '   WebViewExtras1 now has all the methods and properties of WebView1 plus it's additonal methods and properties
    '   so you can use WebView1 to get/set WebView properties/methods
    '   or use WebViewExtras1 to get/set WebView1 properties/methods with the additional properties/method of WebViewExtras

    Dim WebViewClient1 As DefaultWebViewClient
    WebViewClient1.Initialize("WebViewClient1")
    WebViewExtras1.JavaScriptEnabled=True
    WebViewExtras1.SetWebViewClient(WebViewClient1)
    'This row is useless
    'WebViewExtras1.AddJavascriptInterface(WebViewClient1,"B4A")
  
  
    Dim JavascriptInterface1 As DefaultJavascriptInterface
    JavascriptInterface1.Initialize
    WebViewExtras1.AddJavascriptInterface(JavascriptInterface1,"B4A")
  
    WebViewExtras1.CanGoback()

    WebViewExtras1.LoadUrl("https://192.168.0.106/MobileOffice/D2548BA4-E6FD-43A0-9423-F4AE0BC7B710/Phone/Auth/Authorize/" & pId.GetDeviceId)
  
End Sub


Sub WebViewGoBack_Request()
    WebViewExtras1.GoBackOrForward(-1)
    Log(9)
End Sub
 
Upvote 0
Top