headbanner

Menu:

nigel-1

Quick Notes:

April 20th, 2008:
Ruby is all syntactic sugar once you get into it. Its not a bad thing, but one should remember that. Rails does too much at run time, and I'm missing JavaScript. Need to finish my XUL based XDebug client for PHP.

Read more...

About Nigel

Nigel is a consultant at ThoughtWorks, seeking a code free nirvana. Unfortunately he's missed the second left.

Previous Posts

- Interesting PHP function scoping caveat

- Implementing a Singleton Design pattern in PHP

- Unicode Rendering in Firefox is broken.

- Questions for the Campers at BarCampMumbai2

- GTAC 2007, Selenium, and the UI as objects in test...

- Installing Xdebug for PHP on Centos 4 (server)

- Learning Javascript the right way.

Archives

- August 2007
- October 2007
- January 2008
- February 2008
- March 2008

My choice of links
worth visiting

Check out

- BookEazy
- Intermission
- Sukshma
- CodeWord
- TechCrunch

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]

Hi. You've reached Nigel.in

This site is still in its very early stages, but what you have landed on, possibly inadvertently, is the website of a certain curious character a.k.a Nigel Fernandes

The aim of this page was to serve as a landing point for those of you who want to know a little more about me. The links on the right and left will take you deeper my world, or possibly on to different exciting things.

Its a big web out there and this is just one more street for you to saunter down. I hope you like it.

Close this.

My Photo
Name:
Location: Panjim, Goa, India

Crazy, dancing, programming, Goan.. I'm a computer geek and proud to be one. I program in Java, Ruby and .Net, PHP, Javascript. A lot of my recent work has been about CSS and UI design practices for large scale websites and Agile teams. I still while away hours dreaming up a web startup.

 

Dynamic Javascript source includes in XUL Applications and Firefox Extenstions

Friday, February 29, 2008

So you want to load a snippet of JavaScript code dynamically from the context of a script executing in your FireFox Application.

The cleanest way to achieve this is to use the rather little known mozIJSSubScriptLoader interface. You can see the documentation for the interface here.

The beauty of this method is that you can load the snippet or code block of JavaScript into a defined scope. Neat! no messy globals or eval() calls.

For example:
Lets say that you have a file test.js that can be accessed at the Chrome path chrome://yourextensionname/content/test.js and it contains the following:
function Foo () {
this.baz = "deep value";
}

Foo.protoype = {
readBaz : function () {
alert(this.baz);
}
}

Lets say you want to load this test.js file into your application. I suggest creating a function that can load this code into a defined scope as below:
function includeJS(chromeFilePath, NameSpaceContainer) {
var scriptLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
.getService(Components.interfaces.mozIJSSubScriptLoader);
scriptLoader.loadSubScript(chromeFilePath, NameSpaceContainer);
}

You could then use this includeJS() as show below:
var baseURI = "chrome://yourextensionname/";
var MyApplicationNameSpace = {};

try {
includeJS(baseURI+"content/test.js",MyApplicationNameSpace);
} catch (e) {
alert("Could not load Namespace: Encountered excpetion "+e);
}

var myFooObj = new MyApplicationNameSpace.Foo();
myFooObj.readBaz(); // will alert "deep value"


As great as this method of loading Javascript is, there are other ways you could consider. You could for instance use Mozilla's io-service coupled with an eval() as below:
function includeJS(chromeFilePath, NameSpaceContainer) {

var IOService = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
var IOStream = Components.classes["@mozilla.org/scriptableinputstream;1"]
.getService(Components.interfaces.nsIScriptableInputStream);
var data = IOService.newChannel(chromeFilePath, null, null).open();
IOStream.init(data);
var dataStr = IOStream.read(data.available());
IOStream.close();
data.close();
eval(dataStr, NameSpaceContainer);
}

The problem with both these two methods is that they break when you try to load a Javascript source file which contains special Unicode characters. As a work around for this I suggest using a
technique borrowed from buzzword technology AJAX ;-) , and do a XmlHttpRequest call to a Chrome URL.

For example you could use the following code as replacement to the two previous includeJS() methods:
function includeJS(chromeFilePath, NameSpaceContainer) {
var httpRequest = new XMLHttpRequest();
httpRequest.onload = function() {
eval(this.responseText,NameSpaceContainer);
};
httpRequest.open("GET",chromeFilePath, true);
httpRequest.send(null);
}
This seems to work even for source files that contain funky Unicode characters. You could replace the Chrome URl with file paths relative to your system, but I'd recommend always using a Chrome URL where possible. Its a better, more file system agnostic approach.

Labels: , , ,


Comments:
Is there a way that I can load the javascript from within the javascript as you explained but not from Chrome path but from my server instead.

Please reply at kochhark@gmail.com

Thank you
 
Post a Comment

Subscribe to Post Comments [Atom]





<< Home