JavaScript Snippet - Event Isolation With Documentation

Posted at


/*
https://developer.mozilla.org/en-US/docs/Web/API/Event/stopImmediatePropagation - on the lowest-target: stops other listeners of the same type-event.
https://developer.mozilla.org/en-US/docs/Web/API/Event/cancelBubble - same as "stopImmediatePropagation" (old)
https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault - on the window down to target: stops other listeners of the same type-event to execute + marks the event as canceled.
https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation - on the window down to to target: stops other listeners of the same type-event on same target from executing + stops all listeners of the same type-event on target's ancestor from executing. similar to "preventDefault" but it allows the current listener to execute, plus it does not cancel the event.
*/
function isolate(ev){
("boolean" === typeof ev.cancelBubble) && (ev.cancelBubble = true );
("function" === typeof ev.stopImmediatePropagation) && (ev.stopImmediatePropagation(), ev.stopImmediatePropagation.call(ev.currentTarget));
("function" === typeof ev.preventDefault) && (ev.preventDefault(), ev.preventDefault.call( ev.currentTarget));
("function" === typeof ev.stopPropagation) && (ev.stopPropagation(), ev.stopPropagation.call( ev.currentTarget));

/* return false; */ /* include this line at the end of your listener */
}