JavaScript Ninja - Proper Prevention Of Event-Bubbling And Default-Actions

Posted at

Explanation By Example.



function handler(ev){
"function" === typeof ev.preventDefault && ev.preventDefault();
"function" === typeof ev.stopPropagation && ev.stopPropagation();

/* useful if you're handling a CustomEvent - before firing it, after event has fired those attributes are (usually) read-only. */
ev.cancelBubble = true; /* * */
ev.cancelable = true; /* * */
ev.bubbles = false; /* * */

/* -------- */
/* do stuff */
/* -------- */

return false;
}