### Get Data from JavaScript Function

In this example, the data from a JavaScript Function is displayed in the B4J Log.
Solution 1
Get the data from a function using JavaScript alert.
HTML
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display an alert box - the output will be shown on the B4J Log.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
alert("I am an alert box!");
}
</script>
</body>
</html>
B4J
`Sub Process_Globals
Private wv As WebView
...
End Sub`
`Sub wv_Event(MethodName As String, Args() As Object)
Log("MethodName : " & MethodName )
Log("Number of Arguments: " & Args.Length)
Dim joWebEvent As JavaObject
joWebEvent= Args(0)
Log("Data:")
Log(joWebEvent.RunMethod("getData",Null))
End Sub`
Output:
Program started.
MethodName : handle
Number of Arguments: 1
Data:
I am an alert box!
Solution 2
Get the data from a function using JavaScript alert and show dialog using library jRLDialogs8.
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display an alert box - the output will be shown on the B4J Log and a dialog is shown.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
//This will write data that can be obtained using WebEngine API Method getData
alert("GO");
}
</script>
</body>
</html>
B4J
`Sub Process_Globals
Private wv As WebView
Private Dlg As Dialogs8
...
End Sub`
`Sub wv_Event(MethodName As String, Args() As Object)
GetTheData(MethodName, Args)
End Sub`
`Sub GetTheData(MethodName As String, Args() As Object)
Dim joWebEvent As JavaObject
joWebEvent= Args(0)
Dim s As String = joWebEvent.RunMethod("getData",Null)
If s.EqualsIgnoreCase("GO") Then
Dlg.InformationDialog("", "", "Hi from WebView")
End If
End Sub`

Changed: , Category: WebView, File: T6C284M2130.htm, ID: 2130