简体   繁体   中英

global ajax handlers jquery

i'm using global ajax handlers like

$(document).ajaxError(function(e, xhr, settings, exception) { 
});

$(document).ajaxStart(function(e, xhr, settings, exception) {
    $(".spinner").show();
})

$(document).ajaxComplete(function(e, xhr, settings, exception) {
    $(".spinner").hide();   
})

and it works well.

But i have some content which loaded from server by ajax, and with this content i'm loading some javascript which makes new ajax request. And when this ajax request start - global handlers not working :(

Description

You should use jQuery's live() or on() method to bind your handlers. This will work for new elements.

If you use jQuery 1.7 you should use the .on() method, if not use the .live()

Sample

live()

$(document).live("ajaxStart", function(e, xhr, settings, exception)  {
    $(".spinner").show();  
});

$(document).live("ajaxComplete", function(e, xhr, settings, exception)  {
    $(".spinner").hide();  
});

on()

$(document).on("ajaxStart", function(e, xhr, settings, exception)  {
    $(".spinner").show();  
});

$(document).on("ajaxComplete", function(e, xhr, settings, exception)  {
    $(".spinner").hide();  
});

More Information

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM