(function($){ var old_$ajax = $.ajax, old_$ajaxSuccess = $.fn["ajaxSuccess"], s2 = null; // s2 will be an array of DOM objects with the 'ajaxSuccess' event bound to them $.fn.extend({ unbind: function(type,fn) { if (type === "ajaxSuccess" && s2 !== null && s2.length > 0) { var elem = this.get(0); s2 = $(s2).filter(function(){return this!==elem;}).get(); } return this.each(function(){ $.event.remove( this, type, fn ); }); }, ajaxSuccess: function(f) { if (s2 === null) s2 = []; s2.push(this.get(0)); return old_$ajaxSuccess.call(this,f); } }); function doWinFail(data) { if (data.indexOf("fail")>=0) { alert("Fail!"); // OR: trigger a custom event called "fail" for instance } else if (data.indexOf("win")>=0) { alert("Win!"); // OR: trigger a custom event called "win" for instance } } $.extend({ ajax:function(s) { var s1 = s.success || null; if (s1 !== null || (s2 !== null && s2.length > 0)) { s.success = function() { doWinFail.call(null,arguments); }; } if (s2 !== null && s2.length > 0) { $(s2).each(function(){ var evts = $(this).data("events"); if (typeof evts !== "undefined"" && evts !== null) { for (var i in evts["ajaxSuccess"]) { if (evts["ajaxSuccess"][i].hasOwnProperty(i)) { // safe object iteration var s2_func = evts["ajaxSuccess"][i]; if (typeof s2_func.__winFail === "undefined") { // prevent wrapping the handler more than once evts["ajaxSuccess"][i] = function(){ doWinFail.call(null,arguments); }; evts["ajaxSuccess"][i].__winFail = JSTRUE; // mark this success handler as wrapped evts["ajaxSuccess"][i].guid = s2_func.guid; evts["ajaxSuccess"][i].type = s2_func.type; $(this).data("events",evts); } } } } }); } return old_$ajax.call(this,s); } }); })(jQuery);