This question is for the purposes of developing jQuery plugins and other self-contained JavaScript snippets that don't require modifying other script files for compatibility.
We all know that event.preventDefault() will prevent the default event so we can run a custom function. But what if we want to simply delay the default event before invoking it? I've seen various, case-specific ninja tricks and workarounds to re-invoke the default action, but like I said, my interest is in a universal way to re-trigger the default, and not deal with default triggers on a case-by-case basis.
$(submitButton).click(function (e) { e.preventDefault(); // Do custom code here. e.invokeDefault(); // Imaginary... :( });
Even for something as simple as form submission, there seems to be no universal answer. The $(selector).closest("form").submit()
workaround assumes that the default action is a standard form submission, and not something wacky like a __doPostBack()
function in ASP.NET. To the end of invoking ASP.NET callbacks, this is the closest I've come to a universal, set-it-and-forget-it solution:
$(submitButton).click(function (e) { e.preventDefault(); // Do custom code here. var javascriptCommand = e.currentTarget.attributes.href.nodeValue; evalLinkJs(javascriptCommand); }); function evalLinkJs(link) { // Eat it, Crockford. :) eval(link.replace(/^javascript:/g, "")); }
I suppose I could start writing special cases to handle normal links with a window.location
redirect, but then we're opening a whole new can of worms--piling on more and more cases for default event invocation creates more problems than solutions.
So how about it? Who has the magic bullet that I've been searching for?
Note: The preventDefault() method does not prevent further propagation of an event through the DOM. Use the stopPropagation() method to handle this.
preventDefault() The preventDefault() method of the Event interface tells the user agent that if the event does not get explicitly handled, its default action should not be taken as it normally would be.
event. defaultPrevented. The property event. defaultPrevented is true if the default action was prevented, and false otherwise.
We can prevent this default behaviour by making a small modification to the definition of the handleSubmit function. We call a preventDefault on the event when submitting the form, and this will cancel the default event behavior (browser refresh) while allowing us to execute any code we write inside handleSubmit.
Take a look at this one:
You could try
if(!event.mySecretVariableName) { event.preventDefault(); } else { return; // do nothing, let the event go } // your handling code goes here event.originalEvent.mySecretVariableName = "i handled it"; if (document.createEvent) { this.dispatchEvent(event.originalEvent); } else { this.fireEvent(event.originalEvent.eventType, event.originalEvent); }
Using this answer: How to trigger event in JavaScript? and the jQuery event reference: http://api.jquery.com/category/events/event-object/
Tag the event object you receive so if you receive it again you don't loop.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With