Execute javascript from code

stratus

Active Member
Licensed User
Longtime User
I want to execute this js function
"onClick="javascript:theURL='pharmacyshow.asp?pharmacyid=7192&programmeid=1';OpenWin(theURL);" href="javascript:void(0);"
from basic4android.Is this possible?
 

MaaMoz

Member
Licensed User
Longtime User
Execute Java Script from b4a

Can I execute the following code?
If yes, what is wrong... it does not work.

Dim wwx As WebViewExtras

wwx.executeJavascript(WebView1,"Document.GetElementById('tdId').InnerText='Test1'")

Thank you for looking into this
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Can I execute the following code?
If yes, what is wrong... it does not work.

Dim wwx As WebViewExtras

wwx.executeJavascript(WebView1,"Document.GetElementById('tdId').InnerText='Test1'")

Thank you for looking into this

Javascript is case sensitive, assuming that the webpage loaded into WebView1 contains an element with an id of 'tdId' you'd want to execute:

B4X:
wwx.executeJavascript(WebView1, "document.getElementById('tdId').innerText='Test1';")

If that fails then it could be because the WebView doesn't support the innerText property.
See this page W3C DOM Compatibility - HTML, you might might want to use innerHTML instead:

B4X:
wwx.executeJavascript(WebView1, "document.getElementById('tdId').innerHTML='Test1';")

As you're already using WebViewExtras in your project you can add the WebChromeClient to your WebView:

B4X:
wwx.addWebChromeClient(WebView1)

Any javascript errors will now appear in the log viewer - makes debugging things like this much easier.

Martin.
 
Upvote 0

MaaMoz

Member
Licensed User
Longtime User
Java Code from b4a

Sorry messed up my answer by pressing wrong Key:

Dear Martin,

Thank you for your answer.
Silly how quickly one misses sintax. :sign0013:

BY THE WAY:

Using b4a for just a few days.
EXCELLENT program working with an excellent system (Android)

Greetings
MaaMoz
 
Upvote 0

MaaMoz

Member
Licensed User
Longtime User
Hi Martin

General Question about using WebViewExtras and executing Java Code with it:

I wanted to use two (2) WebViews on the same activity.

When I tried to use B4A.CallSub... the Sub was NOT being called.
When I removed the second WebView, the B4A.CallSub... worked properly.

Is it thus not possible to use two WebViews simultaneously and use addJavascriptInterface / B4A.CallSub?

B4A is excellent!
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi.

Looking at the source code for WebViewExtras i see no reason why you cannot add a JavascriptInterface to each of two WebViews in the same Activity.

Try this:

Use the addWebChromeClient method to add a WebChromeClient to either the WebView where the Javascript is not working OR even try adding it to both WebViews.

Any javascript errors will now be redirected to the Android Log.

Run your project again and look at the Log - do you see any relevant error messages reported?

Can you upload your project so i can take a look myself?

Martin.
 
Upvote 0

MaaMoz

Member
Licensed User
Longtime User
Version 1.9 Problem?

Hi Martin,

I am wondering if the problem I mentioned in my previous message is not related to Updating to Version 1.9.

If I use below code in Version 1.9 and B4A.Bridge 2.0 then the CallSub.B4A does not work.

Is my code wrong?

If I use the same code in Version 1.8 then it works!

---------Code -----------------------------
'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 wb As WebView
Dim wvx As WebViewExtras

End Sub

Sub Activity_Create(FirstTime As Boolean)

'Dim x As String : x = "<!DOCTYPE html PUBLIC " & Chr(34) & "-//W3C//DTD XHTML 1.0 Transitional//EN" & Chr(34) & " " & Chr(34) & "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" & Chr(34) & ">"

wb.Initialize("wb")
Activity.AddView(wb,0,0,100%x,100%y)

wvx.addJavascriptInterface(wb,"B4A")
wvx.addWebChromeClient(wb)

wb.LoadHtml("<html><head></head><body><br /><a href=" & Chr(34) & "javascript:void(0)" & Chr(34) & " onclick=" & Chr(34) & "B4A.CallSub('xTest', true, 'Test1')" & Chr(34) & ">Touch me 1</a><br /><a href=" & Chr(34) & "javascript:void(0)" & Chr(34) & " onclick=" & Chr(34) & "B4A.CallSub('xTest', true, 'Test2')" & Chr(34) & ">Touch me 2</a></body></html>")
wb.BringToFront

End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub xTest(strTest As String)
Msgbox(strTest,"OK")
End Sub

------------ END Code

Info: I used string x in front of "<html>" ... still the code does not work in Version 1.9

What is wrong?
 
Upvote 0

MaaMoz

Member
Licensed User
Longtime User
Version 1.9 Problem?

Forgot to mention in my previous post:

N.B.:

In Version 1.8 the code works in DEBUG Mode with B4A_Bridge but NOT when installed on my Galaxy Tab 7.

Could it be that it uses diferent compilers for Debug Mode and Real Compiling?

Thank you
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Hi there.

Take a look at the attached project:

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 wb, wb2 As WebView
Dim wvx As WebViewExtras

End Sub

Sub Activity_Create(FirstTime As Boolean)

   'Dim x As String : x = "<!DOCTYPE html PUBLIC " & Chr(34) & "-//W3C//DTD XHTML 1.0 Transitional//EN" & Chr(34) & " " & Chr(34) & "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" & Chr(34) & ">"

   wb.Initialize("wb")
   Activity.AddView(wb, 0, 0, 100%x, 50%y-1dip)

   wb2.Initialize("")
   wvx.addJavascriptInterface(wb2, "B4A")
   wvx.addWebChromeClient(wb2)
   Activity.AddView(wb2, 0, 50%y, 100%x, 50%y)
   wb2.LoadUrl("file:///android_asset/wb2_page.htm")

   wvx.addJavascriptInterface(wb, "B4A")
   wvx.addWebChromeClient(wb)

   wb.LoadHtml("<html><head></head><body><br /><a href=" & Chr(34) & "javascript:void(0)" & Chr(34) & " onclick=" & Chr(34) & "B4A.CallSub('xTest', true, 'Test1')" & Chr(34) & ">Touch me 1</a><br /><a href=" & Chr(34) & "javascript:void(0)" & Chr(34) & " onclick=" & Chr(34) & "B4A.CallSub('xTest', true, 'Test2')" & Chr(34) & ">Touch me 2</a></body></html>")
   wb.BringToFront

End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub xTest(strTest As String)
   Msgbox(strTest,"OK")
End Sub

The Activity displays 2 WebViews and each one has a javascript interface and webchromeclient added.

Tested on a Froyo emulator using B4A v1.9 in both debug and release modes and it works fine.

Also tested on my ZTE Blade running CyanogenMod 2.3.7 and works fine.

As RickyD found in the other thread, the problem is more likely your javascript syntax than with WebViewExtras.

Martin.
 

Attachments

  • maamoz.zip
    6.3 KB · Views: 634
Upvote 0

MaaMoz

Member
Licensed User
Longtime User
WebViewExtras...

Thank you warwound for checking into this.
Will yet have to look at the code you sent me.

MaaMoz
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
THIS IS AN (nearly) THREE YEARS OLD THREAD!
 
Upvote 0
Top