4

I have a question regarding events.

Basically, I want to run a jquery method when the user clicks a link to leave the page. In this method I want to check a few things before they leave, so I know I can call:

e.preventDefault();

But what if I want the event to continue after I prevent the event?

Any suggestions how to approach this?

Thank you!

Base33
  • 3,167
  • 2
  • 27
  • 31

1 Answers1

4

If the method is synchronous you can just call it from within the event handler. The default action will not be executed until the handler returns, so you have as much time as you want to decide if you want to prevent it or not.

If the method is async things get more complicated. Since it will not be possible to wait on the method you will have to unconditionally prevent the default action and trigger it again conditionally -- probably with .trigger() or .triggerHandler() -- after the async method has completed.

Jon
  • 428,835
  • 81
  • 738
  • 806