X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;ds=sidebyside;f=ajax%2Fajax.js;h=bd08f83e0a005f3ff67ea616bba363e9c3b24af0;hb=56992c6ad95eb9323b325099dda828a5039def5b;hp=87eadbf675339abf52e0eb8ab72260766c4b694a;hpb=8a4a1edf047f2c272f663866eb7b5fcd644d65b3;p=jquery.git diff --git a/ajax/ajax.js b/ajax/ajax.js index 87eadbf..bd08f83 100644 --- a/ajax/ajax.js +++ b/ajax/ajax.js @@ -3,86 +3,120 @@ // http://jquery.com/docs/ajax/ if ( typeof XMLHttpRequest == 'undefined' && typeof window.ActiveXObject == 'function') { - var XMLHttpRequest = function() { - return new ActiveXObject((navigator.userAgent.toLowerCase().indexOf('msie 5') >= 0) ? - "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP"); - }; + var XMLHttpRequest = function() { + return new ActiveXObject((navigator.userAgent.toLowerCase().indexOf('msie 5') >= 0) ? + "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP"); + }; } $.xml = function( type, url, data, ret ) { - var xml = new XMLHttpRequest(); + var xml = new XMLHttpRequest(); - if ( xml ) { - xml.open(type || "GET", url, true); + if ( xml ) { + xml.open(type || "GET", url, true); - if ( data ) - xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); + if ( data ) + xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); - if ( ret ) - xml.onreadystatechange = function() { - if ( xml.readyState == 4 ) ret(xml); - }; + xml.onreadystatechange = function() { + if ( xml.readyState == 4 ) { + if ( ret ) ret(xml); + $.triggerAJAX( $.httpData(xml) ); + } + }; - xml.send(data) - } + xml.send(data) + } }; $.httpData = function(r,type) { - return r.getResponseHeader("content-type").indexOf("xml") > 0 || type == "xml" ? - r.responseXML : r.responseText; + return r.getResponseHeader("content-type").indexOf("xml") > 0 || type == "xml" ? + r.responseXML : r.responseText; }; $.get = function( url, ret, type ) { - $.xml( "GET", url, null, function(r) { - if ( ret ) ret( $.httpData(r,type) ); - }); + $.xml( "GET", url, null, function(r) { + if ( ret ) ret( $.httpData(r,type) ); + }); }; $.getXML = function( url, ret ) { - $.get( url, ret, "xml" ); + $.get( url, ret, "xml" ); }; $.post = function( url, data, ret, type ) { - $.xml( "POST", url, $.param(data), function(r) { - if ( ret ) ret( $.httpData(r,type) ); - }); + $.xml( "POST", url, $.param(data), function(r) { + if ( ret ) ret( $.httpData(r,type) ); + }); }; $.postXML = function( url, data, ret ) { - $.post( url, data, ret, "xml" ); + $.post( url, data, ret, "xml" ); +}; + +// Global AJAX Event Binding +// Requested here: +// http://jquery.com/discuss/2006-March/000415/ + +$.fn.handleAJAX = function( callback ) { + $.ajaxHandles = $.merge( $.ajaxHandles, this.cur ); + return this.bind( 'ajax', callback ); +}; + +$.ajaxHandles = []; +$.triggerAJAX = function(data){ + for ( var i = 0; i < $.ajaxHandles.length; i++ ) + triggerEvent( $.ajaxHandles[i], 'ajax', [data] ); +}; + +// Dynamic Form Submission +// Based upon the mailing list post at: +// http://jquery.com/discuss/2006-March/000424/ + +$.fn.serialize = function(callback) { + return this.each(function(){ + var a = {}; + $(this) + .find("input:checked,hidden,text,option[@selected],textarea") + .filter(":enabled").each(function() { + a[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value; + }); + $.xml( this.method || "GET", this.action || "", $.param(a), callback ); + }); }; $.param = function(a) { - var s = []; - for ( var i in a ) - s[s.length] = i + "=" + encodeURIComponent( a[i] ); - return s.join("&"); + var s = []; + for ( var i in a ) + s[s.length] = i + "=" + encodeURIComponent( a[i] ); + return s.join("&"); }; $.fn.load = function(a,o,f) { - // Arrrrghhhhhhhh!! - // I overwrote the event plugin's .load - // this won't happen again, I hope -John - if ( a && a.constructor == Function ) - return this.bind("load", a); - - var t = "GET"; - if ( o && o.constructor == Function ) { - f = o; o = null; - } - if (o != null) { - o = $.param(o); - t = "POST"; - } - var self = this; - $.xml(t,a,o,function(h){ - var h = h.responseText; - self.html(h).find("script").each(function(){ - try { - eval( this.text || this.textContent || this.innerHTML ); - } catch(e){} - }); - if(f)f(h); - }); - return this; + // Arrrrghhhhhhhh!! + // I overwrote the event plugin's .load + // this won't happen again, I hope -John + if ( a && a.constructor == Function ) + return this.bind("load", a); + + var t = "GET"; + if ( o && o.constructor == Function ) { + f = o; + o = null; + } + if (o != null) { + o = $.param(o); + t = "POST"; + } + var self = this; + $.xml(t,a,o,function(h){ + var h = h.responseText; + self.html(h).find("script").each(function(){ + try { + eval( this.text || this.textContent || this.innerHTML ); + } catch(e){} + }); + if(f)f(h); + }); + return this; };