javascriptdebuggingfirefoxfirefox-addonxul

Variable is not defined error in my Firefox extension


I'm developing a Firefox add-on. When I run it, I open up the browser console and it says, AMO_Uedit_Beta_Firefox is not defined :browser.xul.

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://Uedit/skin/skin.css" type="text/css"?>
<!DOCTYPE Uedit SYSTEM "chrome://Uedit/locale/translations.dtd">
<overlay id="Uedit-overlay" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    <script src="Uedit.js" />
    <toolbarpalette id="BrowserToolbarPalette">
        <toolbarbutton id="Uedit" class="toolbarbutton-1" label="Edit HTML" tooltiptext="Edit HTML" oncommand="AMO_Uedit_Beta_Firefox.Uedit()" />
    </toolbarpalette>
</overlay>

The toolbar button that calls a function which is part of an object (AMO_Uedit_Beta_Firefox).

I double-checked the names and they both match. Is it because the script doesn't load properly? I'm sure it's not that variable names can't start with capital letters.

var AMO_Uedit_Beta_Firefox={ // This is for "wrapping the loose variables."

Both the file's references are the exact same. Could it be because the script didn't load at all?

<toolbarbutton id="Uedit" class="toolbarbutton-1" label="Edit HTML" tooltiptext="Edit HTML" oncommand="AMO_Uedit_Beta_Firefox.Uedit()" />

I tried changing the relative URL (<script src="Uedit.js" />) to an absolute URL (<script src="chrome://Uedit/Uedit.js" />) in the browser.xul, but now it just returns a blank error message.

enter image description here Weird blank error message.

These errors cause the rest of the add-on to not work at all, so I can't continue developing it until this is fixed. What're some possible solutions?

EDIT:

I figured out a solution. I have to put a line of JavaScript before the first statement.

var AMO_Uedit_Beta_Firefox = { // Will not work!
    ...

If I put a console.log in the front, for example.

console.log("");
var AMO_Uedit_Beta_Firefox = { // This will work!
    ...

The only question is, why does this work?


Solution

  • I figured out a solution. I have to put a line of JavaScript before the first statement.

    var AMO_Uedit_Beta_Firefox = { // Will not work!
        ...
    

    If I put a console.log in the front, for example.

    console.log("foo bar");
    var AMO_Uedit_Beta_Firefox = { // This will work!
        ...
    

    I solved the problem myself, although the solution is strange.