# Class JavascriptContext

java.lang.Object
com.codename1.javascript.JavascriptContext

public class JavascriptContext extends java.lang.Object

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");`

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final boolean`
  `DEBUG`
  Flag to enable/disable logging to a debug log.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `JavascriptContext(BrowserComponent c)`
  Creates a Javascript context for the given BrowserComponent.

- ## Method Summary

  Modifier and Type
  Method
  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(String jsFunc, JSObject self, java.lang.Object[] params)`
  Calls a Javascript function with the given parameters.
  `java.lang.Object`
  `call(String jsFunc, JSObject self, java.lang.Object[] params, boolean async, Callback callback)`
  Calls a Javascript function with the given parameters, and optionally to make the call asynchronously.
  `java.lang.Object`
  `call(String jsFunc, JSObject self, java.lang.Object[] params, boolean async, SuccessCallback callback)`
  Calls a Javascript function with the given parameters, and optionally to make the call asynchronously.
  `void`
  `callAsync(JSObject func, JSObject self, java.lang.Object[] params, Callback callback)`
  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, java.lang.Object[] params, SuccessCallback callback)`
  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, java.lang.Object[] params, Callback callback)`
  Calls a Javascript function with the given parameters asynchronously.
  `void`
  `callAsync(String jsFunc, JSObject self, java.lang.Object[] params, SuccessCallback callback)`
  Calls a Javascript function with the given parameters asynchronously.
  `void`
  `cleanup()`
  Cleans up stale references to Javascript objects.
  `java.lang.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.
  `void`
  `getAsync(String javascript, Callback callback)`
  Returns the result of the provided javascript expression asynchronously.
  `void`
  `getAsync(String javascript, SuccessCallback callback)`
  Returns the result of the provided javascript expression asynchronously.
  `JSObject`
  `getWindow()`
  Returns a reference to the Javascript "window" object.
  `void`
  `set(String key, java.lang.Object value)`
  Sets a Javascript value given a compatible Java object value.
  `void`
  `set(String key, java.lang.Object value, boolean async)`
  Sets a Javascript value given a compatible Java object value.
  `void`
  `setAsync(String key, java.lang.Object value)`
  Sets a Javascript value given a compatible Java object value asynchronously.
  `final void`
  `setBrowserComponent(BrowserComponent c)`
  Sets the BrowserComponent on which this javascript context runs.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### DEBUG

    public static final boolean DEBUG
    Flag to enable/disable logging to a debug log.
    See Also:
      - Constant Field Values

- ## Constructor Details

  - ### JavascriptContext

    public JavascriptContext(BrowserComponent c)

    Creates a Javascript context for the given BrowserComponent.

    ###### Parameters

    - `c`

- ## Method Details

  - ### setBrowserComponent

    public final void setBrowserComponent(BrowserComponent c)

    Sets the BrowserComponent on which this javascript context runs.

    ###### Parameters

    - `c`: The BrowserComponent on which the context runs.

  - ### cleanup

    public void cleanup()
    Cleans up stale references to Javascript objects. This is triggered randomly whenever a JSObject is constructed (with a given probability threshold). If this is never called then the JS GC won't be able to free any objects that have ever been wrapped by JSObject because they are stored in the global lookup table.

  - ### get

    public java.lang.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

    - `javascript`: The javascript to be executed.

    ###### Returns

    The result of the javascript expression.

  - ### getWindow

    public JSObject getWindow()

    Returns a reference to the Javascript "window" object.

    ###### Returns

    The window object.

  - ### getAsync

    public void getAsync(String javascript, Callback callback)

    Returns the result of the provided javascript expression asynchronously.

    ###### Parameters

    - `javascript`: A javascript expression.

    - `callback`: Callback to be called with the result of the expression.

  - ### getAsync

    public void getAsync(String javascript, SuccessCallback callback)

    Returns the result of the provided javascript expression asynchronously.

    ###### Parameters

    - `javascript`: A javascript expression.

    - `callback`: Callback to be called with the result of the expression.

  - ### set

    public void set(String key, java.lang.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

    - `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

    public void set(String key, java.lang.Object value, boolean async)

    Sets a Javascript value given a compatible Java object value. This is an abstraction upon javascript to execute `key = value`. See `set(java.lang.String, java.lang.Object)` for a full description.

    ###### Parameters

    - `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.

    ###### See also

    - #set(java.lang.String, java.lang.Object)

    - #setAsync(java.lang.String, java.lang.Object)

  - ### setAsync

    public void setAsync(String key, java.lang.Object value)

    Sets a Javascript value given a compatible Java object value asynchronously.

    ###### Parameters

    - `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.

    ###### See also

    - #set(java.lang.String, java.lang.Object)

  - ### call

    public 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. 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

    - `func`: The Javascript function object that is being called.

    - `self`: Javascript Object that should act as "this" for the function call.

    - `params`: @param 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.

    ###### Returns

    Returns:
      The result of the function call. Javascript results will be automatically converted to their associated Java types.

  - ### callAsync

    public void callAsync(JSObject func, JSObject self, java.lang.Object[] params, Callback callback)

    Calls a Javascript function (encapsulated in a JSObject) with a specified Javascript Object as the "this" context for the function call.

    ###### Parameters

    - `func`: The Javascript function object that is being called.

    - `self`: Javascript Object that should act as "this" for the function call.

    - `params`: @param 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.

  - ### callAsync

    public void callAsync(JSObject func, JSObject self, java.lang.Object[] params, SuccessCallback callback)

    Calls a Javascript function (encapsulated in a JSObject) with a specified Javascript Object as the "this" context for the function call.

    ###### Parameters

    - `func`: The Javascript function object that is being called.

    - `self`: Javascript Object that should act as "this" for the function call.

    - `params`: @param 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.

  - ### call

    public java.lang.Object call(String jsFunc, JSObject self, java.lang.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

    - `jsFunc`: @param 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`: @param params Array of the Javascript parameters, as Java objects. These use the same conversions as are described in the docs for set().

    ###### Returns

    Returns:
      Returns the return value converted to the corresponding Java object type.

  - ### callAsync

    public void callAsync(String jsFunc, JSObject self, java.lang.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

    - `jsFunc`: @param 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`: @param params Array of the Javascript parameters, as Java objects. These use the same conversions as are described in the docs for set().

    - `callback`: @param callback Callback to pass the return value converted to the corresponding Java object type.

  - ### callAsync

    public void callAsync(String jsFunc, JSObject self, java.lang.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

    - `jsFunc`: @param 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`: @param params Array of the Javascript parameters, as Java objects. These use the same conversions as are described in the docs for set().

    - `callback`: @param callback Callback to pass the return value converted to the corresponding Java object type.

  - ### call

    public java.lang.Object call(String jsFunc, JSObject self, java.lang.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

    - `jsFunc`: @param 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`: @param 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.

    ###### Returns

    Returns:
      Returns the return value converted to the corresponding Java object type. This will always return null if `async` is `true`.

  - ### call

    public java.lang.Object call(String jsFunc, JSObject self, java.lang.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

    - `jsFunc`: @param 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`: @param 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.

    ###### Returns

    Returns:
      Returns the return value converted to the corresponding Java object type. This will always return null if `async` is `true`.