Software Development

JavaScript Occasion.defaultPrevented

JavaScript Occasion.defaultPrevented
Written by admin

Whether or not you began with the outdated on_____ property or addEventListener, you realize that occasions drive consumer experiences in fashionable JavaScript. Should you’ve labored with occasions, you realize that preventDefault() and stopPropagation() are often used to deal with occasions. One factor you in all probability did not know: there is a defaultPrevented proptery on occasions!

Contemplate the next block of code:

// Particular to a hyperlink
const hyperlink = doc.querySelector('#my-link');
hyperlink.addEventListener('click on', e => e.preventDefault());

// A bigger doc scope
doc.addEventListener('click on', documentClickHandler);
operate documentClickHandler(occasion) {
    if (occasion.defaultPrevented) {// Utilizing the property
        // Do one factor if the clicking has been dealt with
    }
    else {
        // In any other case do one thing recent
    }
}

When preventDefault is known as on a given occasion, the defaultPrevented property will get toggled to true. As a result of occasion propagation, the occasion bubbles upward with this defaultPrevented worth.

I have been dealing with occasions for 20 years and did not know this property existed till now. What’s nice about defaultPrevented is that it stays with the occasion without having to trace observe it globally!

  • Serving Fonts from CDN

    For optimum efficiency, everyone knows we should put our belongings on CDN (one other area).  Together with these belongings are customized net fonts.  Sadly customized net fonts through CDN (or any cross-domain font request) do not work in Firefox or Web Explorer (appropriately so, by spec) although…

  • 5 Awesome New Mozilla Technologies You’ve Never Heard Of

    My journey to Mozilla Summit 2013 was unbelievable.  I’ve spent a lot time specializing in my undertaking that I had overlooked the entire nice work Mozillians have been placing out.  MozSummit supplied the proper reminder of how sensible my colleagues are and the way a lot…

  • Editable Content Using MooTools 1.2, PHP, and MySQL

    All people and their aerobics teacher needs to have the ability to edit their very own web site lately. And why would not they? I imply, they’ve a $500 price range, no HTML/CSS expertise, and extraordinary expectations. Sufficient ranting although. Having an internet site that enables for…

  • Dynamically Create Charts Using MooTools MilkChart and Google Analytics

About the author

admin

Leave a Comment