Visit Mozilla.org

mozIJSSubScriptLoader

From MDC

The mozIJSSubScriptLoader interface can be used from JS to load and run JavaScript code from the given URL at run-time.

Contents

mozIJSSubScriptLoader is defined in js/src/xpconnect/idl/mozIJSSubScriptLoader.idl. It is scriptable and has been frozen since 2002-08-26.

Implemented by: @mozilla.org/moz/jssubscript-loader;1

Inherits from: nsISupports

To create, use:

var subscriptLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
                      .getService(Components.interfaces.mozIJSSubScriptLoader);

[edit] Method overview

<any JS type> loadSubScript (in wstring url[, targetObj]);

[edit] Methods

[edit] loadSubScript()

<any JS type> loadSubScript (in wstring url[, targetObj]);

Synchronously loads and executes the script from the specified URL.

The specified script is executed with the system principal, meaning that there are no restrictions on what it can do.

This method must only be called from JavaScript!

[edit] Parameters

url
The URL pointing to the script to load. It must be a chrome: URL, and must be local. Other URL types (file: and resource:) are not allowed for security reasons.
Note: Previous versions of Firefox allowed the URL to be file: and resource: as well, but this was changed in Firefox 3
targetObj
Optional The object to use as the scope object for the script being executed. It defaults to the global object of the caller.

Examples:

var global = this;
var obj = {};
var loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
                       .getService(Components.interfaces.mozIJSSubScriptLoader);
loader.loadSubScript("data:text/plain,var a=1", obj)
loader.loadSubScript("data:text/plain,this.b=1", obj)
loader.loadSubScript("data:text/plain,c=1", obj)
loader.loadSubScript("data:text/plain,function f(){}", obj)

alert(obj.toSource()); // ({a:1, b:1, f:function f() {}})
alert("a" in global); // false
alert("b" in global); // false
alert(global.c); // 1

[edit] Return value

The value returned by the sub-script. This can be any JavaScript value.