public class JavascriptContext
extends java.lang.Object
Typically you would obtain a context for a BrowserComponent via its constructor, passing the BrowserComponent to the context.
E.g.
WebBrowser b = new WebBrowser();
BrowserComponent bc = (BrowserComponent)b.getInternal();
JavascriptContext ctx = new JavascriptContext(bc);
JSObject window = (JSObject)ctx.get("window");
Modifier and Type | Field and Description |
---|---|
static boolean |
DEBUG
Flag to enable/disable logging to a debug log.
|
Constructor and Description |
---|
JavascriptContext(com.codename1.ui.BrowserComponent c)
Creates a Javascript context for the given BrowserComponent.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Object |
call(JSObject func,
JSObject self,
java.lang.Object[] params)
Calls a Javascript function (encapsulated in a JSObject) with a specified
Javascript Object as the "this" context for the function call.
|
java.lang.Object |
call(java.lang.String jsFunc,
JSObject self,
java.lang.Object[] params)
Calls a Javascript function with the given parameters.
|
java.lang.Object |
get(java.lang.String javascript)
Executes a javascript string and returns the result of the execution as
an appropriate object value depending on the type of value that was returned.
|
void |
set(java.lang.String key,
java.lang.Object value)
Sets a Javascript value given a compatible Java object value.
|
void |
setBrowserComponent(com.codename1.ui.BrowserComponent c)
Sets the BrowserComponent on which this javascript context runs.
|
public JavascriptContext(com.codename1.ui.BrowserComponent c)
c
- public final void setBrowserComponent(com.codename1.ui.BrowserComponent c)
c
- The BrowserComponent on which the context runs.public java.lang.Object get(java.lang.String javascript)
Return value types will depend on the Javascript type returned. The following table shows the mappings:
Javascript Type | Java Return Type |
---|---|
Number | java.lang.Double |
String | java.lang.String |
Boolean | java.lang.Boolean |
Object | JSObject |
Function | JSObject |
null | null |
undefined | null |
//Get the window object
JSObject window = (JSObject)ctx.get("window");
// Create a new empty Javascript Object
JSObject newObj = (JSObject)ctx.get("{}");
// Get the current document body contents as a string.
String html = (String)ctx.get("document.body.innerHTML");
// Get a numerical result
Double result = (Double)ctx.get("1+2");
// Get a Javascript function object
JSObject func = (JSObject)ctx.get("function(a,b){ return a+b }");
// Get a boolean result
Boolean res = (Boolean)ctx.get("1 < 2");
javascript
- The javascript to be executed.public void set(java.lang.String key, java.lang.Object value)
key = value
.
The key is any Javascript expression whose result can be assigned. The value is a Java object that will be converted into a Javascript object as follows:
Java type | Converted to |
---|---|
Double | Number |
Integer | Number |
Float | Number |
Long | Number |
String | String |
JSObject | Object by ref |
null | null |
Hence if you want to set a Javascript string value, you can just pass a Java string into this method and it will be converted.
You may notice that if you pass a JSObject as the value parameter, the table above indicates that it is passed by reference. A JSObject merely stores a reference to a Javascript object from a lookup table in the Javascript runtime environment. It is this lookup that is ultimately assigned to the "key" when you pass a JSObject as the value. This has the effect of setting the actual Javascript Object to this value, which is effectively a pass-by-reference scenario.
// Set the window.location.href to a new URL
ctx.set("window.location.href", "http://google.com");
// Create a new JSObject, and set it as a property of another JSObject
JSObject camera = (JSObject)ctx.get("{}");
ctx.set("window.camera", camera);
// Set the name of the camera via JSObject.set()
camera.set("name", "My Camera");
// Get the camera's name via Javascript
String cameraName = (String)ctx.get("window.camera.name");
// Should be "My Camera"
// Set the camera name via context.set()
ctx.set("camera.name", "New name");
String newName = (String)camera.get("name");
// Should be "New name"
key
- A javascript expression whose result is being assigned the value.value
- The object or value that is being assigned to the Javascript variable
on the left.public java.lang.Object call(JSObject func, JSObject self, java.lang.Object[] params)
This operates almost exactly like the Javascript Function.apply() method.
Note that JSObject also has a couple of call()
methods
that may be more convenient to use as they will automatically set the "self"
parameter to the JSObject callee. This version of the method is handy in cases
where you have been passed a function (perhaps as a callback) and you need to
execute that function in a particular context.
// Get the Array.push method as an object
JSObject push = (JSObject)ctx.get("Array.prototype.push");
// Create a new array
JSObject colors = (JSObject)ctx.get("['red', 'green', 'blue']");
// "Push" a new color onto the array directly using the JSObject's call()
// method
colors.call("push", "purple");
// Alternate method using JavascriptContext.call()
ctx.call(push, colors, "orange");
// Check size of colors array now
Double size = (Double)colors.get("length");
// Should be 5.0
// Get 4th color (should be purple)
String purple = (String)colors.get(3);
// Get 5th color (should be orange)
String orange = (String)colors.get(4);
func
- The Javascript function object that is being called.self
- Javascript Object that should act as "this" for the function call.params
- The parameters that should be passed to the function. These
parameters should be passed as Java objects but will be converted into their
associated Javascript version.public java.lang.Object call(java.lang.String jsFunc, JSObject self, java.lang.Object[] params)
jsFunc.call(self, param1, param1, ..., paramn)
jsFunc
- A javascript expression that resolves to a function object that
is to be called.self
- The Javascript object that is used as "this" for the method call.params
- Array of the Javascript parameters, as Java objects. These use
the same conversions as are described in the docs for set().