Webviewextras expose onclick to B4A

Jamie

Member
Licensed User
Longtime User
Hi.
I hope I can explain this clearly enough. here goes...
I have an .html file that I load into a webview using httputils2. I create the file on my server with php and assign buttons to Invoice numbers which are then put into a table.
B4X:
echo "<td><button id=$InvNo onclick=myFunction($InvNo)>$InvNo</button></td>";
When I click a button in my webview, myFunction changes the document.title to $InvNo.
B4X:
<script>
function myFunction(InvNo)
{
  document.title = InvNo;
}
</script>
In B4A I can add a button to the panel that holds the webview and do this to get the invoice number(button) that I click in the webview.
B4X:
Sub btnWVinv_Click
   WebInterface.executeJavascript(wv, "B4A.CallSub('SavePageTitle',true, document.title)")
End Sub
My question is can I get the value from the button in the webview without having to use a second button on the panel to get the value?
Thanks
 

warwound

Expert
Licensed User
Longtime User
Update your javascript function, so that it sends the button id to a b4a Sub:

B4X:
<script>
function myFunction(InvNo)
{
  document.title = InvNo;
  B4A.CallSub('SavePageTitle', true, InvNo);
}
</script>

Your Sub SavePageTitle should handle the button click and invoice number.

Martin.
 
Upvote 0

Jamie

Member
Licensed User
Longtime User
Even better I can skip the script and put the sub call right in the onclick if I want because I really only need the invno. I was changing the document.title because I saw a thread where someone changed the title so they could get access to a variable.

Sent from a galaxy far far away!
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
I am trying to detect a click event on webview. I believe it is possible with webviewextras library by injecting a javascript but I can't seem to figure out how. I tried adding the following javascript to the end of HTML code that is displayed in the webview and it doesn't work. I probably got some syntax of javascript wrong as I have no experience with that language.

B4X:
wex.addJavascriptInterface(wb,"B4A")
htmlcode=htmlcode & "<script type='text/javascript'> function initialize(){B4A.CallSub('wb_Click'); }</script>"
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
The javascript that your webpage executes has choice of syntaxes to call the CallSub() method:

CallSub(subName As String, callUIThread As boolean)
CallSub(subName As String, callUIThread As boolean, parameter1 As String)
CallSub(subName As String, callUIThread As boolean, parameter1 As String, parameter2 As String)
CallSub(subName As String, callUIThread As boolean, parameter1 As String, parameter2 As String, parameter3 As String)

See http://www.b4x.com/forum/additional-libraries-classes-official-updates/12453-webviewextras.html

You posted javascript does not specify a callUIThread parameter, details of what callUIThread does can be found on the above link.

Also your < script > tag shoudl be within the html element of the webpage - not simply appended to the end of the HTML.

Now you want to detect a click on a webpage - you want to detect a click anywhere on the rendered webpage or on just an element within the webpage?

This javascript will add a click listener to the entire rendered webpage (the document body element) and call a B4A Sub named 'wb_Click', that Sub can modify the UI of your Activity as true is passed as the value for callUIThread.

B4X:
document.body.onclick=function(){
  B4A.CallSub('wb_Click', true);
};

If you passed false as the parameter and then wb_Click tried to modify the Activity UI you'd get an exception.

You'd be best to wait for your webpage to load - wait until the WebView raises it's PageFinished event - and then inject the javascript into the webpage:

B4X:
Sub MyWebView_PageFinished (Url As String)
 MyWebViewExtras.executeJavascript(MyWebView, "document.body.onclick=function(){B4A.CallSub('wb_Click', true);")
End Sub

Martin.
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
Thanks for the help Martin. Unfortunately that didn't work. wb_Click isn't getting called even though the javascript injection is happening correctly.

Anyway leave it. I found an alternative way to avoid webview clicks completely.
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
No I did do that before trying injection, but still didn't work; probably due to some careless mistake from my part.
 
Upvote 0

welu1805

Active Member
Licensed User
Longtime User
Sub MyWebView_PageFinished (Url As String)
MyWebViewExtras.executeJavascript(MyWebView,
"document.body.onclick=function(){B4A.CallSub('wb_Click', true);")
End Sub

This thread is almost 2 years old but I also was looking for a solution to get a "Click-event" on a webview. In the code above is a mistake: A bracket "}" is missing. Here is the correct code which works:

MyWebViewExtras.executeJavascript(MyWebView, "document.body.onclick=function(){B4A.CallSub('wb_Click', true)};")

I hope that helps someone.
 
  • Like
Reactions: omo
Upvote 0
Top