Friday, May 26th, 2006
Subscript Loader: Handling JS scope
<>p>Weird Al over at MozillaZine has written up a little known Mozilla feature known as the JavaScript subscript loader in: JavaScript Scope through "subscript loaders".Using this you can better handle scoping issues in JavaScript (albeit Mozilla only for now).
-
-
const scopeRegistry = {
-
subscriptLoader: Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
-
.getService(Components.interfaces.mozIJSSubScriptLoader),
-
registeredScopes: {},
-
getScope: function getScope(aScopeId) {
-
if (typeof this.registeredScopes[aScopeId] == "undefined")
-
this.registeredScopes[aScopeId] = {};
-
return this.registeredScopes[aScopeId];
-
},
-
loadScriptByScope: function loadScriptByScope(aURL, aScopeId) {
-
if (aScopeId) {
-
var scopeObj = this.getScope(aScopeId);
-
this.subscriptLoader.loadSubScript(aURL, scopeObj);
-
} else {
-
this.subscriptLoader.loadSubScript(aURL);
-
}
-
}
-
}
-
Related Content:











Hmm, does this solve some problem not already solved by JSAN?
JSAN.use('Foo.Bar');
…that looks a lot easier to me, and JSAN is reasonably cross-browser-happy. Am I missing something?
Seems like this might be a handy way to load external libraries in a GreaseMonkey script? Anyone (with more time on their hands than me) played with this?