- Download JSandbox.
<link rel="jsandbox" href="path/to/jsandbox-worker.js" />anywhere in your document. I recommend putting it in the document's
- Read the API documentation below.
This example code demonstrates the JSandbox API.
- Firefox 3.5+
- Google Chrome 4+
Instead of using a
<link> tag, you may define
JSandbox.url to specify the location
of the JSandbox worker script.
All of these methods can be accessed on the
JSandbox constructor (in one-use sandboxes)
options.callbackis a function, it is passed the results as long as no errors occur. If
options.onerroris a function and an error occurs, it is passed the error object. The code is
eval()ed in a top-level pseudo-function-scope. If you define a variable using a
varstatement, the variable is private to the eval.
thisis still the global object. If this method is called on
JSandboxobject is returned. Otherwise, the ID of the request is returned.
Executes code in a faster method than
eval, but does not pass a return value to the callback function (though the function is still called if defined). Unlike
eval, the code is run in the global scope (
options.datais a string,
options.datawill attempt to be loaded in the sandbox. If
options.datais an array, every string it contains will attempt be loaded. If
options.onerroris a function and an error is thrown while parsing a script or a script could not be resolved,
options.onerroris passed the error object. Otherwise,
options.callbackis called when the scripts are finished loading.
These methods can only be on JSandbox instances:
- Aborts a pending request with the ID,
- Terminates the worker thread and any pending requests are aborted. You cannot use the JSandbox instance on which you called this method after it is called.
The following are all of the properties that may be included in an
In the case of
exec, it is the code to execute. In the case of
load, it is an array of the script(s) to load. If you only need to load one script, just pass a string instead.
- The input data available to the code via the input variable. The input should be JSON-convertible.
- The callback to pass the return value of the executed code if no exceptions were thrown.
- The callback to pass an exception if one is thrown upon executing the code.
Any method that takes an options object can also be called using the following positional-arguments syntax:
someMethod(data [, callback] [, input] [, onerror]);
JSandbox object can also be referenced as