Run the function only if the default value is not specified /

Is there a way to run a function only if event.preventDefault () is called in an event (another unknown function). This is for the jQuery plugin, so I don't know what other parts of the page can do. I tried this:

Event.test = Event.preventDefault; Event.preventDefault = function () { alert('Success'); this.test(); } 

but it doesn’t work ... it just behaves as usual, without errors.

And vice versa, I also want the opposite ... to call the function only if event.preventDefault () is not called. Essentially, add a function to the default action for the event. Any ideas? Is all this possible?

Edit: based on the comment, I have a solution to the first problem: http://jsfiddle.net/nathan/VAePB/9/ . It works in Chrome ( function preventDefault() { [native code] } notifications, but IE warns undefined ). Therefore, IE will not allow me to define Event.prototype.test, but it will allow me to override Event.prototype.preventDefault. might come up with a solution to a second problem based on this if I can just get it to work in IE.

+4
source share
2 answers

For the first problem, try something like this:

 oldPreventDefault = Event.prototype.preventDefault; Event.prototype.preventDefault = function() { //do stuff oldPreventDefault.call(this); } 

I don't know if this will work, but it may be worth it.

For the second problem, I would try something similar to real-time event processing. Place the listener on the parent element (i.e. body or top level div ). If you can get your hook in preventDefault as above, you can use it to set a flag. If the event bubbles up to this element and your flag is not set, do the extended behavior. Although this will not work with all events, as not all events bubble. Another way to solve this problem could be to delay execution until the current stack finishes using setTimeout(0,...) and then checks the flag.

0
source

I'm not sure I get it. Can't you just use event.isDefaultPrevented() like this

+3
source

All Articles