Bug Tracker

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#11758 closed enhancement (worksforme)

off(): $(document).off() does not disable event handlers, binded via another $(document).on() handler

Reported by: chestozo Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.7.2
Keywords: Cc:
Blocked by: Blocking:

Description

Here I wrote a little demo for the bug: http://jsbin.com/ofejov/edit#source

I know, that this is not really a bug, better called - it is a design issue. When someone uses off() he is not able to disable any handler that is already in events queue because events queue for delegate and live events is composed before real handlers are called, here: https://github.com/jquery/jquery/blob/master/src/event.js#L447

And if some handler off() method is called - queue is not modifed and this allows "disabled" handlers to be triggered.

Change History (4)

comment:1 Changed 7 years ago by dmethvin

Resolution: worksforme
Status: newclosed

You've just attached two event handlers to document there, of course they'll both run. The names you've given the handler functions have no bearing on anything. If you only want the first to run, use evt.stopImmediatePropagation().

http://jsbin.com/ofejov/2/

comment:2 Changed 7 years ago by dmethvin

Just to quote some spec:

http://www.w3.org/TR/DOM-Level-3-Events/#event-flow

Next, the implementation must determine the current target's candidate event listeners. This must be the list of all event listeners that have been registered on the current target in their order of registration. [HTML5] defines the ordering of listeners registered through event handler attributes. Once determined, the candidate event listeners cannot be changed; adding or removing listeners does not affect the current target's candidate event listeners.*

comment:3 Changed 7 years ago by chestozo

Yes, works like in specs, thank you.

Maybe, it would be great to have some force option to force event prevention by .off() method. Because in some large applications it can be difficult to analyze, "why am I calling off() method and it does not work?".

In my real example I cannot disable all click events by calling stopImmediatePropagation() - someone may be interested in this click. All I want - is to disable some specific handler. Like here: http://jsbin.com/ofejov/3/edit

comment:4 Changed 7 years ago by dmethvin

Sorry but it doesn't make sense to add and document a disableStandardEventBehavior option for such an edge case. Use your own flags, for example a .data() item on the element.

Note: See TracTickets for help on using tickets.