0

This is about a Firefox extension:

I have a XUL browser overlay which includes all my javascripts. Eventually a javascript function triggers the load of a xul page. This page contains a button and this button is manipulated from the same javascript which triggered the page load (i.e. set the oncommand attribute of the button dynamically so that it will execute another javascript function).

The problem is, when the button is clicked, it gives me errors. My javascript objects (the namespace, functions, etc.) are not defined. But I can't include all Javascripts again in the xul page, because I need some form of a global object.

Even if I set the button to do a simpler command which has nothing to do with my own code (i.e. gBrowser.loadURI(...)), it says gBrowser is not defined.

How can I call my javascript functions with this button?

1 Answer 1

1

//Access JavaScript from multiple XUL files

change gBrowser to be Services.wm.getMostRecentWindow('navigator:browser').gBrowser

Another option is to create a JSM file and then addEventListener to this page when it loads. Create a JSM file. See example here on gists.github

Sign up to request clarification or add additional context in comments.

2 Comments

awesome, thanks a lot. The Services.wm. ... trick does not work, it even says Services is undefined. But your JSM idea is nice. I read about it here: developer.mozilla.org/en-US/docs/Mozilla/… ... it was enough to refactor my namespace setup js into a jsm module and import it in every xul file. the listener was not needed
cool man. Services doesn't exist because you need to import services like this Cu.import('resource://gre/modules/Services.jsm'); and Cu is const { classes: Cc, interfaces: Ci, utils: Cu } = Components; this is just FYI, the jsm module is far superior to this one as this just gets the most recent window thats it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.