jQuery relies heavily on the use of the document
variable. In a Firefox extension, you are often in a context where the variables document
, and window
, etc. are not defined, or are not defined as the <document>
or <window>
you are expecting them to be. You can usually solve this type of issue by defining window
and document
appropriately.
Without additional information, it is not possible to provide something that is guaranteed to work for you. You have not stated what scope you are working in, nor described the <document>
that you desire to operate upon. Thus, the code below provides a generic solution that will find a <window>
and <document>
which will work in most circumstances, but you might be wanting them to be something else.
Firefox add-ons generally run in a scope where there are multiple object which could be reasonably referred to by the window
and document
variables. If those variables are defined and, if defined, what objects they actually refer to, depends on how the portion of your code that is currently running was entered. Even if they are defined, they are often not defined as the <window>
, or <document>
which jQuery would be expecting (the <window>
/<document>
of the content for the current tab). You will probably need to obtain a reference to both objects for the most recently accessed window/tab.
If a browser window exists (in some instances you could be running where no browser window exists, yet, e.g. at start-up), you can obtain a reference to the most recent browser window
, document
, and gBrowser
with:
if (window === null || typeof window !== "object") {
//If you do not already have a window reference, you need to obtain one:
// Add/remove a "/" to comment/un-comment the code appropriate for your add-on type.
/* Add-on SDK:
var window = require('sdk/window/utils').getMostRecentBrowserWindow();
//*/
//* Overlay and bootstrap (from almost any context/scope):
var window=Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator)
.getMostRecentWindow("navigator:browser");
//*/
}
if (typeof document === "undefined") {
//If there is no document defined, get it
var document = window.content.document;
}
if (typeof gBrowser === "undefined") {
//If there is no gBrowser defined, get it
var gBrowser = window.gBrowser;
}
If you are running the code in response to an event (e.g. a button command
event), you can obtain the current window
with:
var window = event.view
The lack of having the global window
and document
variables available, or having them reference objects other than what you are expecting, is something that many people encounter as a problem when writing Firefox add-ons.
Note: If you are wanting to be natively compatible with multi-process Firefox (Electrolysis, or e10s), then gaining access to the content of the current document is more complex. There are shims in place which should make your code continue to work with multi-process Firefox for some time, but they may/will eventually go away. If you are writing new add-on code and you are wanting access to the <document>
, or <window>
associated with the content of a web page, then you should be using a content script of some sort (there are a variety of types). Which type of content script you should use depends on the what type of Firefox add-on you are writing and what you are doing with the script.
References:
nsIWindowMediator
- Working with windows in chrome code
- SDK: window/utils
- SDK: windows
Large portions of this answer were copied from my earlier answers, including this link and here.