public class JavascriptContext
- Object
- JavascriptContext
Represents a Javascript context of a single BrowserComponent. This provides support for executing Javascript in the BrowserComponent, registering Java callbacks to allow Javascript to call Java functions, and returning values from Javascript to Java.
NOTE: The com.codename1.javascript package is now
deprecated. The preferred method of Java/Javascript interop is to use BrowserComponent#execute(java.lang.String), com.codename1.util.SuccessCallback),
BrowserComponent#executeAndWait(java.lang.String), etc.. as these
work asynchronously (except in the XXXAndWait() variants, which use
invokeAndBlock() to make the calls synchronously.
Typically you would obtain a context for a BrowserComponent via its constructor, passing the BrowserComponent to the context.
E.g.
java WebBrowser b = new WebBrowser(); BrowserComponent bc = (BrowserComponent)b.getInternal(); JavascriptContext ctx = new JavascriptContext(bc); JSObject window = (JSObject)ctx.get("window");
Fields
public static final boolean DEBUG = false | Flag to enable/disable logging to a debug log. |
Constructors
public JavascriptContext(BrowserComponent c) | Creates a Javascript context for the given BrowserComponent. |
Methods
Inherited methods
Field details
DEBUG
public static final boolean DEBUG = falseConstructor details
JavascriptContext
public JavascriptContext(BrowserComponent c)Method details
setBrowserComponent
public final void setBrowserComponent(BrowserComponent c)Parameters
cBrowserComponent- The BrowserComponent on which the context runs.
cleanup
public void cleanup()get
public Object get(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.
Return value types will depend on the Javascript type returned. The following table shows the mappings:
Javascript TypeJava Return Type
Numberjava.lang.Double
Stringjava.lang.String
Booleanjava.lang.Boolean
ObjectJSObject
FunctionJSObject
nullnull
undefinednull
Example
java //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");
Parameters
javascriptString- The javascript to be executed.
Returns
getWindow
public JSObject getWindow()Returns
getAsync
public void getAsync(String javascript, Callback callback)Parameters
javascriptString- A javascript expression.
callbackCallback- Callback to be called with the result of the expression.
getAsync
public void getAsync(String javascript, SuccessCallback callback)Parameters
javascriptString- A javascript expression.
callbackSuccessCallback- Callback to be called with the result of the expression.
set
public void set(String key, Object value)Sets a Javascript value given a compatible Java object value. This is an abstraction
upon javascript to execute 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 typeConverted to
DoubleNumber IntegerNumber FloatNumber LongNumber StringString JSObjectObject by ref nullnull
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.
JSObject “By Ref”
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.
Examples
java // 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"
Parameters
keyString- A Javascript expression whose result is being assigned the value.
valueObject- The object or value that is being assigned to the Javascript variable on the left.
set
public void set(String key, Object value, boolean async)key = value. See set(java.lang.String, java.lang.Object) for a full description.Parameters
keyString- A Javascript expression whose result is being assigned the value.
valueObject- The object or value that is being assigned to the Javascript variable on the left.
asyncboolean- If true, the call is made asynchronously.
setAsync
public void setAsync(String key, Object value)Parameters
keyString- A Javascript expression whose result is being assigned the value.
valueObject- The object or value that is being assigned to the Javascript variable on the left.
call
public Object call(JSObject func, JSObject self, Object[] params)Calls a Javascript function (encapsulated in a JSObject) with a specified Javascript Object as the “this” context for the function call. Also passes a set of arguments to the method.
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.
Example
java // 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);
Parameters
funcJSObject- The Javascript function object that is being called.
selfJSObject- Javascript Object that should act as “this” for the function call.
paramsObject[]- 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.
Returns
callAsync
public void callAsync(JSObject func, JSObject self, Object[] params, Callback callback)Parameters
funcJSObject- The Javascript function object that is being called.
selfJSObject- Javascript Object that should act as “this” for the function call.
paramsObject[]- 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.
callbackCallback- The callback to pass the return value to.
callAsync
public void callAsync(JSObject func, JSObject self, Object[] params, SuccessCallback callback)Parameters
funcJSObject- The Javascript function object that is being called.
selfJSObject- Javascript Object that should act as “this” for the function call.
paramsObject[]- 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.
callbackSuccessCallback- The callback to pass the return value to.
call
public Object call(String jsFunc, JSObject self, Object[] params)Calls a Javascript function with the given parameters. This would translate roughly into executing the following javascript:
jsFunc.call(self, param1, param1, ..., paramn)
Parameters
jsFuncString- A javascript expression that resolves to a function object that is to be called.
selfJSObject- The Javascript object that is used as “this” for the method call.
paramsObject[]- Array of the Javascript parameters, as Java objects. These use the same conversions as are described in the docs for set().
Returns
callAsync
public void callAsync(String jsFunc, JSObject self, Object[] params, Callback callback)Calls a Javascript function with the given parameters asynchronously. This would translate roughly into executing the following javascript:
jsFunc.call(self, param1, param1, ..., paramn)
Parameters
jsFuncString- A javascript expression that resolves to a function object that is to be called.
selfJSObject- The Javascript object that is used as “this” for the method call.
paramsObject[]- Array of the Javascript parameters, as Java objects. These use the same conversions as are described in the docs for set().
callbackCallback- Callback to pass the return value converted to the corresponding Java object type.
callAsync
public void callAsync(String jsFunc, JSObject self, Object[] params, SuccessCallback callback)Calls a Javascript function with the given parameters asynchronously. This would translate roughly into executing the following javascript:
jsFunc.call(self, param1, param1, ..., paramn)
Parameters
jsFuncString- A javascript expression that resolves to a function object that is to be called.
selfJSObject- The Javascript object that is used as “this” for the method call.
paramsObject[]- Array of the Javascript parameters, as Java objects. These use the same conversions as are described in the docs for set().
callbackSuccessCallback- Callback to pass the return value converted to the corresponding Java object type.
call
public Object call(String jsFunc, JSObject self, Object[] params, boolean async, Callback callback)Calls a Javascript function with the given parameters, and optionally to make the call asynchronously. This would translate roughly into executing the following javascript:
jsFunc.call(self, param1, param1, ..., paramn)
Parameters
jsFuncString- A javascript expression that resolves to a function object that is to be called.
selfJSObject- The Javascript object that is used as “this” for the method call.
paramsObject[]- Array of the Javascript parameters, as Java objects. These use the same conversions as are described in the docs for set().
asyncboolean- If true, the call will be made asynchronously.
callbackCallback- Used if
asyncis true to pass the return value.
Returns
async is true.call
public Object call(String jsFunc, JSObject self, Object[] params, boolean async, SuccessCallback callback)Calls a Javascript function with the given parameters, and optionally to make the call asynchronously. This would translate roughly into executing the following javascript:
jsFunc.call(self, param1, param1, ..., paramn)
Parameters
jsFuncString- A javascript expression that resolves to a function object that is to be called.
selfJSObject- The Javascript object that is used as “this” for the method call.
paramsObject[]- Array of the Javascript parameters, as Java objects. These use the same conversions as are described in the docs for set().
asyncboolean- If true, the call will be made asynchronously.
callbackSuccessCallback- Used if
asyncis true to pass the return value.
Returns
async is true.