Whether or not you began with the previous on_____
property or addEventListener
, you understand that occasions drive person experiences in trendy JavaScript. When you’ve labored with occasions, you understand that preventDefault()
and stopPropagation()
are incessantly used to deal with occasions. One factor you most likely did not know: there is a defaultPrevented
proptery on occasions!
Take into account 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 press has been dealt with } else { // In any other case do one thing recent } }
When preventDefault
known as on a given occasion, the defaultPrevented
property will get toggled to true
. Resulting from 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 while not having to trace observe it globally!
Designing for Simplicity
Earlier than we get began, it is value me spending a short second introducing myself to you. My identify is Mark (or @integralist if Twitter occurs to be your communication instrument of selection) and I at the moment work for BBC Information in London England as a principal engineer/tech…
iPad Detection Utilizing JavaScript or PHP
The most well liked system on the market proper now appears to be the iPad. iPad this, iPad that, iPod your mother. I am underwhelmed with the system however that does not imply I should not attempt to account for such units on the web sites I create. In Apple’s…