Is .live () possible for ajaxStart und ajaxComplete?

I am using jQuery 1.4.4 and would like to know if I can use .live () for Ajax events like ajaxStart / ajaxComplete. I have associated these events with .bind (), and it still works

jQuery(sourceId).bind("ajaxSend", function(event, xhr, ajaxOptions) { // do something }); equivalent jQuery(sourceId).ajaxSend(function(event, xhr, ajaxOptions) { // do something }); 

Linking to .live () would be better for my use cases. Is it even possible? I read somewhere that the following snippet is not working

 jQuery(sourceId).live("ajaxSend", function(event, xhr, ajaxOptions) { // do something }); 

Thank you for your responses.

+4
source share
2 answers

You cannot do this with live (), although you can do it with a custom event.

The implication with live () is that you add elements dynamically. Dynamically loaded elements must be executed as a result of some event or AJAX callback, so a new event binding is established in the callback event.

 callback event... //code thatadds the new elements... jQuery('selector that identifies the new elements').bind("ajaxSend", function(event, xhr, ajaxOptions) { // do something }); 

You might want to wrap your code in a function.

Check out the comments copied from http://api.jquery.com/live/ here

The .live () method is useful, but because of its special approach, it cannot be simply replaced with .bind () in all cases. Specific differences:

 DOM traversal methods are not supported for finding elements to send to .live(). Rather, the .live() method should always be called directly after a selector, as in the example above. To stop further handlers from executing after one bound using .live(), the handler must return false. Calling .stopPropagation() will not accomplish this. In jQuery 1.3.x only the following JavaScript events (in addition to custom events) could be bound with .live(): click, dblclick, keydown, keypress, keyup, mousedown, mousemove, mouseout, mouseover, and mouseup. As of jQuery 1.4 the .live() method supports custom events as well as all JavaScript events that bubble. As of jQuery 1.4.1 even focus and blur work with live (mapping to the more appropriate, bubbling, events focusin and focusout). As of jQuery 1.4.1 the hover event can be specified (mapping to mouseenter and mouseleave, which, in turn, are mapped to mouseover and mouseout). 
+2
source

Cannot use live instead of binding ...

+1
source

Source: https://habr.com/ru/post/1346897/


All Articles