Android Programming Press on the image to return to the main documentation page.

JSInterface

Written by Martin Pearman

List of types:

JSInterface

JSInterface


Events:

None

Members:


  addConsoleInterface (aWebView As android.webkit.WebView)

  addJSInterface (aWebView As android.webkit.WebView, jsNamespace As String)

  execJS (aWebView As android.webkit.WebView, jsStatement As String)

Members description:

addConsoleInterface (aWebView As android.webkit.WebView)
Adds a new
WebChromeClient instance to a WebView instance.

aWebView - The WebView instance that you want to add the WebChromeClient instance to.

The WebChromeClient will handle javascript console and error messages from the WebView, echoing them to logcat.
You can view the log entries in the B4A IDE or using the B4A Log Viewer.

It is NOT recommended to use the console in a production application.
Use it for development/debugging only.
addJSInterface (aWebView As android.webkit.WebView, jsNamespace As String)
Adds a new B4A javascript interface instance to a WebView instance.
Uses the WebView
addJavascriptInterface method.

aWebView - The WebView instance that you want to add the B4A javascript interface to.

jsNamespace - Javascript namespace, the namespace which you will use to access all B4A javascript interface methods.

B4A example of addJSInterface method:
myJSInterfaceInstance.addJSInterface(myWebViewInstance, "B4A")

The B4A javascript interface contains an overloaded method named 'CallSub'. CallSub can be used to call a B4A Sub (using javascript) with either none, one or two String parameters.
Examples:
myJSNamespace.CallSub("myB4ASubName");
myJSNamespace.CallSub("myB4ASubName""param 1");
myJSNamespace.CallSub("myB4ASubName""param 1""param 2");
Example where jsNamespace is defined "B4A":
B4A.CallSub("myB4ASubName");

If the B4A Sub returns a String then that String is returned to the calling javascript code.
Values (Strings) returned from the B4A javascript interface to javascript will always be of type Object, that is:
typeof jsValueReturnedJSInterface will always be "object".

In some code this won't matter and javascript's internal type conversion will work for you, however there may be times when you need to explicitly convert an Object to a javascript String or Number type.
For example, let's say JSInterface returns an Object with the value "abcdef|qwerty" and that Object is assigned to a variable named "myValue".
If myValue was a String type then you could use the String type's split() method to convert the String into an Array, using the bar character as a separator:
var myArray=myValue.split("|");
You will get a javascript error with this as the Object type has no split() method.

Solution: To convert an Object to a String type concatenate an empty String to the Object:
var myValue=myValue+"";
myValue is now a javascript String type and it can use all native String methods such as split().
Note that the Object type's native toString() method will return an Object with the same value as the original Object - it will NOT convert the Object to a String type.

Conversion of Object types to javascript's Number type can be done using javascript's native methods: parseInt() and parseFloat()
execJS (aWebView As android.webkit.WebView, jsStatement As String)
Execute one or more javascript statements in a WebView.
execJS is a method intended to be called from your B4A code.

aWebView - The WebView instance in which to execute the javascript.

jsStatement - A String containing the javascript statement or (semi-colon separated) statement(s) to execute.

The javascript statement(s) will always be executed within the global scope of your web page.
Examples:
myJSInterfaceInstance.execJS(myWebView, "location.href='http://google.com/'")
myJSInterfaceInstance.execJS(myWebView, "document.body.style.backgroundColor='red';document.body.style.color='green'")
Top