X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=ajax%2Fajax.js;h=fef1e22bd2e92afacd9aef3201c53447cf40a846;hb=d8bad24d34b1d11893555ce35707cd2716d49adc;hp=bd08f83e0a005f3ff67ea616bba363e9c3b24af0;hpb=b9e0b6ff37ab837ef2e031bac23c36ee3007cc46;p=jquery.git diff --git a/ajax/ajax.js b/ajax/ajax.js index bd08f83..fef1e22 100644 --- a/ajax/ajax.js +++ b/ajax/ajax.js @@ -3,25 +3,59 @@ // 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) ? + XMLHttpRequest = function() { + return new ActiveXObject((navigator.userAgent.toLowerCase().indexOf('msie 5') >= 0) ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP"); }; } +// Counter for holding the active query's +$.xmlActive=0; + $.xml = function( type, url, data, ret ) { var xml = new XMLHttpRequest(); if ( xml ) { + // Open the socket xml.open(type || "GET", url, true); - if ( data ) xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); + // Set header so calling script knows that it's an XMLHttpRequest + xml.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); + + /* Force "Connection: close" for Mozilla browsers to work around + * a bug where XMLHttpReqeuest sends an incorrect Content-length + * header. See Mozilla Bugzilla #246651. + */ + if ( xml.overrideMimeType ) + xml.setRequestHeader('Connection', 'close'); + xml.onreadystatechange = function() { + // Socket is openend + if ( xml.readyState == 1 ) { + // Increase counter + $.xmlActive++; + + // Show loader if needed + if ( ($.xmlActive >= 1) && ($.xmlCreate) ) + $.xmlCreate(); + } + + // Socket is closed and data is available if ( xml.readyState == 4 ) { - if ( ret ) ret(xml); - $.triggerAJAX( $.httpData(xml) ); + // Decrease counter + $.xmlActive--; + + // Hide loader if needed + if ( ($.xmlActive <= 0) && ($.xmlDestroy) ) { + $.xmlDestroy(); + $.xmlActive = 0 + } + + // Process result + if ( ret ) + ret(xml); } }; @@ -36,7 +70,7 @@ $.httpData = function(r,type) { $.get = function( url, ret, type ) { $.xml( "GET", url, null, function(r) { - if ( ret ) ret( $.httpData(r,type) ); + if ( ret ) { ret( $.httpData(r,type) ); } }); }; @@ -46,7 +80,7 @@ $.getXML = function( url, ret ) { $.post = function( url, data, ret, type ) { $.xml( "POST", url, $.param(data), function(r) { - if ( ret ) ret( $.httpData(r,type) ); + if ( ret ) { ret( $.httpData(r,type) ); } }); }; @@ -54,41 +88,17 @@ $.postXML = function( url, data, ret ) { $.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] ); + if (a && typeof a == 'object' && a.constructor == Array) { + for ( var i=0; i < a.length; i++ ) { + s[s.length] = a[i].name + "=" + encodeURIComponent( a[i].value ); + } + } else { + for ( var j in a ) { + s[s.length] = j + "=" + encodeURIComponent( a[j] ); + } + } return s.join("&"); }; @@ -96,27 +106,79 @@ $.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 ) + 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) { + if (o !== null) { o = $.param(o); t = "POST"; } var self = this; $.xml(t,a,o,function(h){ - var h = h.responseText; + h = h.responseText; self.html(h).find("script").each(function(){ try { - eval( this.text || this.textContent || this.innerHTML ); + $.eval( this.text || this.textContent || this.innerHTML ); } catch(e){} }); - if(f)f(h); + if(f){f(h);} }); return this; }; + +/** + * function: $.fn.formValues + * usage: $('#frmLogin').formValues() + * docs: Gets form values and creates a key=>value array of the found values (for ENABLED elements!) + */ +$.fn.formValues = function() { + var a = []; + $("input,textarea,option",this).filter(":enabled").each(function(){ + // Skip selects with options which are not selected + if ((this.parentNode.type == 'select-one' || this.parentNode.type == 'select-multiple') && !this.selected) { + return null; + } + + // Skip radio and checkbox elements which are not checked + if ((this.type == 'radio' || this.type == 'checkbox') && !this.checked) { + return null; + } + + // All other elements are valid + a.push({ + name: this.name || this.id || this.parentNode.name || this.parentNode.id, + value: this.value + }); + }); + return a; +}; + +/** + * function: $.update + * usage: $.update('someJQueryObject', 'someurl', 'array'); + * docs: Mimics the ajaxUpdater from prototype. Posts the key=>value array to the url and + * puts the results from that call in the jQuery object specified. + * --> If you set the blnNoEval to true, the script tags are NOT evaluated. + */ +$.update = function(objElement, strURL, arrValues, fncCallback) { + $.post(strURL, arrValues, function(strHTML) { + // Update the element with the new HTML + objElement.html(strHTML); + + // Evaluate the scripts + objElement.html(strHTML).find("script").each(function(){ + try { + $.eval( this.text || this.textContent || this.innerHTML ); + } catch(e) { } + }); + + // Callback handler + if (fncCallback) { fncCallback(); } + }); +};