Can set mouseover html attribute by javascript, but cannot assign function to mouseover property in Firefox

1

Clearly I'm doing something stupid.

    processTextNodes: function processTextNodes(node) { 
    node = node || content.document.body;                    // base node 
    var children = node.childNodes, i = 0;

    while (node = children[i]) { 
        if (node.nodeType == 3 && node.textContent) {        // text node found, replace enclosed text.
            if (node.nodeName == "script") continue;

            /*node.parentNode.setAttribute("onmouseover", "alert(\"AAA\");");*/
            node.parentNode.onmouseover = function(){ alert("AAA") };
            node.textContent = aatel.transliterate(node.textContent);
        }
        if (node.nodeType == 1) {
            if (node.title) node.title = aatel.transliterate(node.title);
            if (node.alt)   node.alt = aatel.transliterate(node.alt);
        }
        processTextNodes(node); 
        i++; 
    }
},

The above is a method from an extension I'm writing. When run as given above I get the following in the error console

Error: uncaught exception: [Exception... "Component is not available"  nsresult: "0x80040111 (NS_ERROR_NOT_AVAILABLE)"  location: "JS frame :: chrome://aatel/content/overlay.js :: processTextNodes :: line 97"  data: no]

In case there's any ambiguity, line 97 is:

node.parentNode.onmouseover = function(){ alert("AAA")};

If I comment out the line assigning a function to .onmouseover and uncomment the setAttribute line right above it, it works as you would expect. The big ugly opaque exception message is not helping me a lot. What am I doing wrong? Javascript isn't a strong point of mine.

javascript
firefox
dom
asked on Stack Overflow Oct 31, 2011 by cikkle • edited Oct 31, 2011 by cikkle

2 Answers

2

Looks like you're trying to use events in a Firefox exension. Try using addEventListener for adding events:

node.parentNode.addEventListener("mouseover", function () {
    alert("AAA");
}, false);
answered on Stack Overflow Oct 31, 2011 by Lekensteyn
0

The onmouseover setter makes certain assumptions. For example, it assumes that the script calling it is running against a Window object. If that's not the case, it will throw.

If your script above is in a JS component in the extension, it's not running against a Window...

answered on Stack Overflow Oct 31, 2011 by Boris Zbarsky

User contributions licensed under CC BY-SA 3.0