Friday, May 26th, 2006

Subscript Loader: Handling JS scope

Category: JavaScript, Tip

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

javascript
< view plain text >
  1. const scopeRegistry = {
  2.   subscriptLoader: Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
  3.                              .getService(Components.interfaces.mozIJSSubScriptLoader),
  4.   registeredScopes: {},
  5.   getScope: function getScope(aScopeId) {
  6.     if (typeof this.registeredScopes[aScopeId] == "undefined")
  7.       this.registeredScopes[aScopeId] = {};
  8.     return this.registeredScopes[aScopeId];
  9.   },
  10.   loadScriptByScope: function loadScriptByScope(aURL, aScopeId) {
  11.     if (aScopeId) {
  12.       var scopeObj = this.getScope(aScopeId);
  13.       this.subscriptLoader.loadSubScript(aURL, scopeObj);
  14.     } else {
  15.       this.subscriptLoader.loadSubScript(aURL);
  16.     }
  17.   }
  18. }

Related Content:

Posted by Dion Almaer at 10:07 am
2 Comments

++++-
4 rating from 48 votes

2 Comments »

Comments feed TrackBack URI

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?

Comment by frosty — May 26, 2006

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?

Comment by Sam Foster — May 30, 2006

Leave a comment

You must be logged in to post a comment.