jQuery.preventDefault(), jQuery.stopPropagation() and jquery.Stopimmediatepropagation()... no jQuery!

Asked

Viewed 488 times

3

There is an implementation crossbrowser for the methods jQuery.Event.preventDefault(), jQuery.Event.stopPropagation() and jQuery.Event.stopImmediatePropagation() that does not require jQuery?

I have a very specific resource that wouldn’t justify relying on jQuery, even if by Cdns, which I don’t trust.

By the source code of jQuery.Event it is possible to see that the three methods are prototyped based on their availability in the object Event.

But for the "unofficial documentation" on MDN, Event.stopPropagation() has browser availability restriction (Internet Explorer 9).

In that stack of SOEN has an implementation to which I felt sufficiently compatible for the jQuery.preventDefault(), but and the others?

The intention is not only know which inventions "microsoftianas" are equivalent to each method of the Event and yes have a definitive answer about possible fallbacks (even by enriching the KB of the site) and when or in which situations *fallbacks** should or should need to be applied.

2 answers

3

.stopPropagation and .preventDefault are different things.

Prevent "default" behavior (default):

When I need to block an action of your "default" behavior in pure Javascript usage return false;, This works for all browsers.

IE has the Event.returnValue which is an invention of Microsoft but the return false; has the same effect.

Cross browser solution: event.preventDefault(); event.returnValue = false;

Stop event propagation (bubling) in DOM tree:

What the return false; does not stop the spread of the event so it can be useful to use:

e.stopPropagation() and event.cancelBubble = true;. The .cancelBubble is the Microsoft version of stopPropagation required in older versions (IE<9).

Cross browser solution: e.stopPropagation(); event.cancelBubble = true;

0

The methods event.preventDefault() and event.stopPropagation() are not present in Internet Explorer 8 or lower versions. If you want to make your site compatible with these versions, you will need to address, in Javascript, the existence of such functions. In relation to event.preventDefault(), you can use the event.returnValue, and in relation to event.stopPropagation(), you can use the event.cancelBubble. There are no other alternatives in other popular browsers, such as Firefox and Chrome.

  • Although I don’t like Javascript I came to this conclusion but if only Event.stopPropagation() needs a differentiated implementation to also consider the cancelBubble, because the SOEN link I quoted is made the same kind of fallback to the Event.preventDefault(), returning false if this is not available in the object?

  • According to the microsoft documentation, the event.preventDefault() is similar to event.returnValue Internet Explorer 8 and earlier.

  • I don’t think you understand the point of the question. It was good that you were willing to bring these two information, although the second was already implicit in the SOEN’s reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.