Activate your free membership today | Log-in

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

Related Content:

Posted by Dion Almaer at 10:07 am
2 Comments

++++-
4.1 rating from 43 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.