BrowserComponent.execute(java.lang.String)
directly.public class JavascriptContext extends Object
NOTE: The com.codename1.javascript
package is now
deprecated. The preferred method of Java/Javascript interop is to use BrowserComponent.execute(java.lang.String)
, 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.
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
Deprecated.
Flag to enable/disable logging to a debug log.
|
Constructor and Description |
---|
JavascriptContext(BrowserComponent c)
Deprecated.
Creates a Javascript context for the given BrowserComponent.
|
Modifier and Type | Method and Description |
---|---|
Object |
call(JSObject func,
JSObject self,
Object[] params)
Deprecated.
Calls a Javascript function (encapsulated in a JSObject) with a specified
Javascript Object as the "this" context for the function call.
|
Object |
call(String jsFunc,
JSObject self,
Object[] params)
Deprecated.
Calls a Javascript function with the given parameters.
|
Object |
call(String jsFunc,
JSObject self,
Object[] params,
boolean async,
Callback callback)
Deprecated.
Calls a Javascript function with the given parameters, and optionally to make the call asynchronously.
|
Object |
call(String jsFunc,
JSObject self,
Object[] params,
boolean async,
SuccessCallback callback)
Deprecated.
Calls a Javascript function with the given parameters, and optionally to make the call asynchronously.
|
void |
callAsync(JSObject func,
JSObject self,
Object[] params,
Callback callback)
Deprecated.
Calls a Javascript function (encapsulated in a JSObject) with a specified
Javascript Object as the "this" context for the function call.
|
void |
callAsync(JSObject func,
JSObject self,
Object[] params,
SuccessCallback callback)
Deprecated.
Calls a Javascript function (encapsulated in a JSObject) with a specified
Javascript Object as the "this" context for the function call.
|
void |
callAsync(String jsFunc,
JSObject self,
Object[] params,
Callback callback)
Deprecated.
Calls a Javascript function with the given parameters asynchronously.
|
void |
callAsync(String jsFunc,
JSObject self,
Object[] params,
SuccessCallback callback)
Deprecated.
Calls a Javascript function with the given parameters asynchronously.
|
void |
cleanup()
Deprecated.
Cleans up stale references to Javascript objects.
|
Object |
get(String javascript)
Deprecated.
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 |
getAsync(String javascript,
Callback callback)
Deprecated.
Returns the result of the provided javascript expression asynchronously.
|
void |
getAsync(String javascript,
SuccessCallback callback)
Deprecated.
Returns the result of the provided javascript expression asynchronously.
|
JSObject |
getWindow()
Deprecated.
Returns a reference to the Javascript "window" object.
|
void |
set(String key,
Object value)
Deprecated.
Sets a Javascript value given a compatible Java object value.
|
void |
set(String key,
Object value,
boolean async)
Deprecated.
Sets a Javascript value given a compatible Java object value.
|
void |
setAsync(String key,
Object value)
Deprecated.
Sets a Javascript value given a compatible Java object value asynchronously.
|
void |
setBrowserComponent(BrowserComponent c)
Deprecated.
Sets the BrowserComponent on which this javascript context runs.
|
public static boolean DEBUG
public JavascriptContext(BrowserComponent c)
c
- public final void setBrowserComponent(BrowserComponent c)
c
- The BrowserComponent on which the context runs.public void cleanup()
public Object get(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 JSObject getWindow()
public void getAsync(String javascript, Callback callback)
javascript
- A javascript expression.callback
- Callback to be called with the result of the expression.public void getAsync(String javascript, SuccessCallback callback)
javascript
- A javascript expression.callback
- Callback to be called with the result of the expression.public void set(String key, 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 void set(String key, Object value, boolean async)
key = value
. See {@link #set(java.lang.String, java.lang.Object) for a full description.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.async
- If true, the call is made asynchronously.set(java.lang.String, java.lang.Object)
,
setAsync(java.lang.String, java.lang.Object)
public void setAsync(String key, Object value)
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.set(java.lang.String, java.lang.Object)
public Object call(JSObject func, JSObject self, 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 void callAsync(JSObject func, JSObject self, Object[] params, Callback callback)
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.callback
- The callback to pass the return value to.public void callAsync(JSObject func, JSObject self, Object[] params, SuccessCallback callback)
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.callback
- The callback to pass the return value to.public Object call(String jsFunc, JSObject self, 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().public void callAsync(String jsFunc, JSObject self, Object[] params, Callback callback)
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().callback
- Callback to pass the return value converted to the corresponding Java
object type.public void callAsync(String jsFunc, JSObject self, Object[] params, SuccessCallback callback)
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().callback
- Callback to pass the return value converted to the corresponding Java
object type.public Object call(String jsFunc, JSObject self, Object[] params, boolean async, Callback callback)
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().async
- If true, the call will be made asynchronously.callback
- Used if async
is true to pass the return value.async
is true
.public Object call(String jsFunc, JSObject self, Object[] params, boolean async, SuccessCallback callback)
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().async
- If true, the call will be made asynchronously.callback
- Used if async
is true to pass the return value.async
is true
.