开发者

Firefox Extension cannot override element events

开发者 https://www.devze.com 2023-04-08 09:16 出处:网络
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 s

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 d开发者_StackOverflow社区oesn'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.)

  • https://developer.mozilla.org/en/XPCNativeWrapper#Limitations_of_XPCNativeWrapper
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号