简体   繁体   中英

Firefox Extension cannot override element events

I'm having trouble trying to override a form element's onsubmit event. I have no problem adding a listener with addEventListener , but for my particular case, I need to replace the onsubmit but for some reason when I do, it gives me this error:

Error: Component is not available = NS_ERROR_NOT_AVAILABLE

My code is simply this:

gBrowser.contentDocument.getElementById("theform").onsubmit = function() {
   return false;
};

Essentially I want to prevent the form from submitting, but this code fails and throws the above error. Using addEventListener to return false doesn't seem to stop the form from submitting.

Thanks.

For security reasons, the object returned by getElementById in an extension is an XPCNativeWrapper around the DOM element; it's not the element itself. This results in some important limitations. More details here:

Assigning to or reading an on* property on an XPCNativeWrapper of a DOM node or Window object will throw an exception. (Use addEventListener instead, and use "event.preventDefault();" in your handler if you used "return false;" before.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM