1

I want to use a Javascript Module (JSM) in a single window of my Xul application, so I can load the resource as I need it.

But, I need to pass the window to the JSM, and I don't know how to do it. Follows my attempt:

In my resource.jsm:

var EXPORTED_SYMBOLS = ["hello"];

function hello(win) {
    win.alert("ALERT FROM JSM!");
}

calling in my window with:

Components.utils.import("resource://module/resource.jsm");
hello(window);

but I get:

win is undefined

in the resource.jsm.

Any idea how to make it work?

The Student
  • 27,520
  • 68
  • 161
  • 264
  • Just out of curiosity, what happens if you first assign `window = window`? I remember hearing about some really strange issues with certain "pre-defined" global variables that were (somehow) settled with a self-assignment... – Platinum Azure May 05 '11 at 18:12
  • @Platinum Azure I got the same error message. – The Student May 05 '11 at 18:24
  • @Platinum Azure I think what you saw were this [page's samples](https://developer.mozilla.org/En/Code_snippets/Modules). – The Student May 05 '11 at 19:27
  • No, it was about setTimeout... Sorry. I knew it was a long shot, hence putting it in a comment rather than an answer. – Platinum Azure May 05 '11 at 21:42

1 Answers1

0

It might be causing problems that you named the parameter for your hello function to be window. While window isn't a reserved word, most browser environments treat it as an unassignable constant of sorts. Try:

function hello( obj ) {
    obj.alert("ALERT FROM JSM!");
}

in your module and then invoke it with hello(window), hello(document.window), or hello(this)


After reading the Javascript Module documentation, it looks like you'll need to create an object within the module and then change it's property by reference. So in your JSM:

var EXPORTED_SYMBOLS = ["params", "hello"];

params = {
  win: this
};

function hello() {
    params.win.alert("ALERT FROM JSM!");
}

Then you'd invoke by first assigning the window to that parameter and then calling the function:

Components.utils.import("resource://module/resource.jsm");
params.win = window;
hello();

Note: I am not familiar enough with JSMs to know if there's a better way to do this, but this should work.

mVChr
  • 49,587
  • 11
  • 107
  • 104
  • Updated answer with an alternate solution. – mVChr May 05 '11 at 19:03
  • Very good observation. But I got `params.win.alert is not a function`. And by declaring `param.win` as `null` I realized that it was not assigned, as I got `params.win is null` on the `alert()` call. Something is wrong before (or on) the assignment.. (I will post here if I find what) – The Student May 05 '11 at 19:31
  • 1
    Oh, gosh, some of my tests were bad because I didn't update the build-id, so it was running something cached, out-of-date. Now it's working, and can even pass the window as a parameter normally. Thanks for work on this with me! – The Student May 05 '11 at 19:58