jQuery.preventDefault (), jQuery.stopPropagation () and jquery.StopImmediatePropagation () ... without jQuery!

3

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

I have a very specific resource that would not justify depending on jQuery, even for CDNs, which I do not trust.

From the jQuery.Event source code you can see that the three methods are prototyped based on their availability on the Event object.

But by "unofficial documentation" in MDN, event.stopPropagation () has browser availability restriction (Internet Explorer 9).

This stack of SOEN has an implementation that I found to be sufficiently compatible for the jQuery.preventDefault () , but what about the others?

The intent is just to know which "microsoftian" inventions are equivalent to each method of the Event, but to have a definite answer about possible fallbacks (even for KB enrichment of the site) and when or in which situations these fallbacks should or should be applied.     

asked by anonymous 27.11.2014 / 12:40

2 answers

3

.stopPropagation and .preventDefault are different things.

Prevent "default" behavior:

When I need to block an action from its "default" behavior in pure JavaScript use return false; , this works for all browsers.

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

  

Solution cross browser: event.preventDefault(); event.returnValue = false;

Stop event propagation (bubling) in the DOM tree:

What return false; does not do is stop the event propagation so it may be useful to use:

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

27.11.2014 / 14:04
0

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

    
27.11.2014 / 13:16