Firefox Add-on Window.navigator.useragent Error: Window Not Defined
Solution 1:
Firefox add-ons generally run in a scope where the global window
object is not defined (if it is defined depends on how the portion of your code that is currently running was entered). If you want to use methods/objects associated with a window object, the easiest way is to obtain a reference to an appropriate window
object. For some/many things it is possible to do so without obtaining such a reference, but it is usually easier to just get a reference to the most recent browser window.
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 || typeofwindow !== "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:varwindow = 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 (typeofdocument === "undefined") {
//If there is no document defined, get itvardocument = window.content.document;
}
if (typeof gBrowser === "undefined") {
//If there is no gBrowser defined, get itvar gBrowser = window.gBrowser;
}
The lack of having the global window
object available is something that many people encounter as a problem.
References:
Post a Comment for "Firefox Add-on Window.navigator.useragent Error: Window Not Defined"