22

I am calling e.stopPropagation() on almost every event that I have for my current application. Is there any way to just stop the propagation for every event without having to explicitly call the method at the start of every function body?

Charles
  • 50,943
  • 13
  • 104
  • 142
alh
  • 2,569
  • 6
  • 29
  • 42
  • 1
    `stopPropagation()` is a function for the `Event` object. It cannot be called globally. – BenM Feb 12 '14 at 09:52
  • stopPropagation() Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event. – Sai Avinash Feb 12 '14 at 09:55
  • 1
    Here's an interesting post on how to dynamically get all the supported event types in run-time so you don't have to create the list manually: http://stackoverflow.com/questions/5848598/jquery-how-can-i-bind-all-events-on-a-dom-element – Jasper Feb 12 '14 at 17:43

4 Answers4

16

You could bind all events (remove the ones you don't need):

$('*').bind('blur change click dblclick error focus focusin focusout hover keydown keypress keyup load mousedown mouseenter mouseleave mousemove mouseout mouseover mouseup resize scroll select submit', function(event){
    event.stopPropagation();
});

Take a look at possible events at jQuery Docs

user151841
  • 17,377
  • 29
  • 109
  • 171
Alex
  • 1,336
  • 10
  • 19
9

No it cannot be declared Globally

The event.stopPropagation() method stops the bubbling of an event to parent elements, preventing any parent event handlers from being executed.

For example, if there is a link with a click method attached inside of a DIV or FORM that also has a click method attached, it will prevent the DIV or FORM click method from firing.

http://api.jquery.com/event.stopPropagation/

Prashobh
  • 9,216
  • 15
  • 61
  • 91
9

To build on Alex's response, in vanilla JS and with a few more events (all the keyboard and click events I could find):

/**
 * Disable all user events on the page
 *
 * @returns {function()} a function to cancel the disabling
 */
const disableAllUserEvents = () => {
    const events = ["click", "contextmenu", "dblclick", "mousedown", "mouseenter", "mouseleave", "mousemove",
        "mouseover", "mouseout", "mouseup", "keydown", "keypress", "keyup", "blur", "change", "focus", "focusin",
        "focusout", "input", "invalid", "reset", "search", "select", "submit", "drag", "dragend", "dragenter",
        "dragleave", "dragover", "dragstart", "drop", "copy", "cut", "paste", "mousewheel", "wheel", "touchcancel",
        "touchend", "touchmove", "touchstart"];

    const handler = event => {
        event.stopPropagation();
        event.preventDefault();

        return false;
    };

    for (let i = 0, l = events.length; i < l; i++) {
        document.addEventListener(events[i], handler, true);
    }

    return () => {
        for (let i = 0, l = events.length; i < l; i++) {
            document.removeEventListener(events[i], handler, true);
        }
    };
};

Edit: Just know that this isn't a secure solution, as those events can be retrieved and canceled: Use Chrome's webkit inspector to remove an event listener

FloG
  • 463
  • 5
  • 9
-3

Try this. It requires jQuery.

$('*').on('click', function(e){
   e.stopPropagation();
});
Joseph
  • 1,003
  • 3
  • 11
  • 25
Rakesh Kumar
  • 2,705
  • 1
  • 19
  • 33