How to continue event propagation after cancelling?

George Mauer picture George Mauer · Oct 18, 2011 · Viewed 80.1k times · Source

When a user clicks a certain link I would like to present them with a confirmation dialog. If they click "Yes" I would like to continue the original navigation. One catch: my confirmation dialog is implemented by returning a jQuery.Deferred object which is resolved only when/if the user clicks the Yes button. So basically the confirmation dialog is asynchronous.

So basically I want something like this:

$('a.my-link').click(function(e) {
  e.preventDefault(); e.stopPropogation();
  MyApp.confirm("Are you sure you want to navigate away?")
    .done(function() {
      //continue propogation of e
    })
})

Of course I could set a flag and re-trigger click but that is messy as heck. Any natural way of doing this?

Answer

c69 picture c69 · Oct 18, 2011

Below are the bits from the code that actually worked in Chrome 13, to my surprise.

function handler (evt ) {
    var t = evt.target;
    ...
    setTimeout( function() {
        t.dispatchEvent( evt )
    }, 1000);
    return false;
}

This is not very cross-browser, and maybe will be fixed in future, because it feels like security risk, imho.

And i don't know what happens, if you cancel event propagation.