最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does getPreventDefault() work but defaultPrevented doesn't? - Stack Overflow

programmeradmin2浏览0评论

I'm working on a Firefox extension, and I keep seeing the following warning:

Use of getPreventDefault() is deprecated. Use defaultPrevented instead.

However, for what I'm trying to do it seems that defaultPrevented doesn't work, while getPreventDefault() does. For a simplified code snippet, here's what I put into my Firefox browser scratchpad while running in the Browser context:

window.addEventListener('fooEvent', function (event) { 
    console.log('fooEvent has fired');
    event.preventDefault();
}, true, true);

So then in my browser console (the one I get when hitting Shift+Ctrl+k) I run the following code:

e = document.createEvent('Event');
e.initEvent('fooEvent', true, true);
document.dispatchEvent(e);
console.log('after dispatch', e.defaultPrevented, e.getPreventDefault());

I can see the fooEvent has fired message in my Javascript console, so I know the event was caught. But I see the message after dispatch false true which indicates that defaultPrevented is still false while getPreventDefault() is correctly returning true. But Firefox still displays the deprecation warning!

I'd like to avoid using a deprecated API call because I don't want a Firefox upgrade to break my extension. (There's an open ticket for the deprecated API call to be removed, though it looks like it'll probably be awhile before it's pleted.) What am I doing wrong?

I'm working on a Firefox extension, and I keep seeing the following warning:

Use of getPreventDefault() is deprecated. Use defaultPrevented instead.

However, for what I'm trying to do it seems that defaultPrevented doesn't work, while getPreventDefault() does. For a simplified code snippet, here's what I put into my Firefox browser scratchpad while running in the Browser context:

window.addEventListener('fooEvent', function (event) { 
    console.log('fooEvent has fired');
    event.preventDefault();
}, true, true);

So then in my browser console (the one I get when hitting Shift+Ctrl+k) I run the following code:

e = document.createEvent('Event');
e.initEvent('fooEvent', true, true);
document.dispatchEvent(e);
console.log('after dispatch', e.defaultPrevented, e.getPreventDefault());

I can see the fooEvent has fired message in my Javascript console, so I know the event was caught. But I see the message after dispatch false true which indicates that defaultPrevented is still false while getPreventDefault() is correctly returning true. But Firefox still displays the deprecation warning!

I'd like to avoid using a deprecated API call because I don't want a Firefox upgrade to break my extension. (There's an open ticket for the deprecated API call to be removed, though it looks like it'll probably be awhile before it's pleted.) What am I doing wrong?

Share Improve this question edited Jul 11, 2019 at 6:59 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jun 11, 2014 at 18:42 Eli CourtwrightEli Courtwright 194k69 gold badges223 silver badges257 bronze badges 6
  • 1 Have you seen this?: bugzilla.mozilla/show_bug.cgi?id=985988 – Nile Commented Jun 11, 2014 at 18:57
  • @Nile: I'm seeing this behavior in Firefox 29, where that bug has already been fixed. I also just confirmed the fix locally by running the snippet in that ticket to confirm that defaultPrevented is true in the alert message. – Eli Courtwright Commented Jun 11, 2014 at 20:00
  • @EliCourtwright: No, that bug report states they didn't fix it in FF29? – Bergi Commented Jun 11, 2014 at 22:34
  • 1 @Bergi: The fix was backported to Firefox 29 (status-firefox29: verified). Was landed on the branches in ment #16. – nmaier Commented Jun 12, 2014 at 8:58
  • 1 @Bergi nah, verified means "verified fixed". Confirmed bugs are "new" instead of "unconfirmed" and statuses usually "affected". – nmaier Commented Jun 12, 2014 at 15:14
 |  Show 1 more ment

1 Answer 1

Reset to default 6

I can reproduce this in Firefox 30 and Nightly.

What you have discovered here seems to be a genuine bug when it es to handling synthetic content events from privileged code.

To answer your question: You don't seem to do anything wrong; the browser is. Use getDefaultPrevented() for the time being and while it is still available. You can feature-detect (and hope that .defaultPrevented will be fixed before .getPreventDefault() is removed):

var dp = "getPreventDefault" in e ?
  e.getPreventDefault() :
  e.defaultPrevented;

As the one who discovered this bug, please file a bug and block it against https://bugzilla.mozilla/show_bug.cgi?id=691151

It would be great if you provided the new bug URI in a ment or something. If you don't want to file yourself, please say so and I will file it for you.

Edit

Another way to deal with it is to use the result of dispatchEvent(). Apparantly the mozilla folks forgot to break it, too.

The return value of dispatchEvent indicates whether any of the listeners which handled the event called preventDefault. If preventDefault was called the value is false, else the value is true.

from: DOM-Level-2-Events

e = document.createEvent('Event');
e.initEvent('fooEvent', true, true);
if (document.dispatchEvent(e)) {
  // Execute default action
}
发布评论

评论列表(0)

  1. 暂无评论