3 // http://jquery.com/docs/ajax/
5 if ( typeof XMLHttpRequest == 'undefined' && typeof window.ActiveXObject == 'function') {
6 XMLHttpRequest = function() {
7 return new ActiveXObject((navigator.userAgent.toLowerCase().indexOf('msie 5') >= 0) ?
8 "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP");
12 // Counter for holding the active query's
15 $.xml = function( type, url, data, ret ) {
16 var xml = new XMLHttpRequest();
20 xml.open(type || "GET", url, true);
22 xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
24 // Set header so calling script knows that it's an XMLHttpRequest
25 xml.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
27 /* Force "Connection: close" for Mozilla browsers to work around
28 * a bug where XMLHttpReqeuest sends an incorrect Content-length
29 * header. See Mozilla Bugzilla #246651.
31 if ( xml.overrideMimeType )
32 xml.setRequestHeader('Connection', 'close');
34 xml.onreadystatechange = function() {
36 if ( xml.readyState == 1 ) {
40 // Show loader if needed
41 if ( ($.xmlActive >= 1) && ($.xmlCreate) )
45 // Socket is closed and data is available
46 if ( xml.readyState == 4 ) {
50 // Hide loader if needed
51 if ( ($.xmlActive <= 0) && ($.xmlDestroy) ) {
66 $.httpData = function(r,type) {
67 return r.getResponseHeader("content-type").indexOf("xml") > 0 || type == "xml" ?
68 r.responseXML : r.responseText;
71 $.get = function( url, ret, type ) {
72 $.xml( "GET", url, null, function(r) {
73 if ( ret ) { ret( $.httpData(r,type) ); }
77 $.getXML = function( url, ret ) {
78 $.get( url, ret, "xml" );
81 $.post = function( url, data, ret, type ) {
82 $.xml( "POST", url, $.param(data), function(r) {
83 if ( ret ) { ret( $.httpData(r,type) ); }
87 $.postXML = function( url, data, ret ) {
88 $.post( url, data, ret, "xml" );
91 $.param = function(a) {
93 if (a && typeof a == 'object' && a.constructor == Array) {
94 for ( var i=0; i < a.length; i++ ) {
95 s[s.length] = a[i].name + "=" + encodeURIComponent( a[i].value );
99 s[s.length] = j + "=" + encodeURIComponent( a[j] );
105 $.fn.load = function(a,o,f) {
107 // I overwrote the event plugin's .load
108 // this won't happen again, I hope -John
109 if ( a && a.constructor == Function ) {
110 return this.bind("load", a);
114 if ( o && o.constructor == Function ) {
123 $.xml(t,a,o,function(h){
125 self.html(h).find("script").each(function(){
127 $.eval( this.text || this.textContent || this.innerHTML );
136 * name: $.fn.formValues
137 * example: $('#frmLogin').formValues('sButton')
138 * docs: Gets form values and creates a key=>value array of the found values.
139 * Optionally adds the button which is clicked if you provide it.
140 * Only does this for ENABLED elements in the order of the form.
142 $.fn.formValues = function(sButton) {
144 var elp = {INPUT:true, TEXTAREA:true, OPTION:true};
147 $('*', this).each(function() {
148 // Skip disabled elements
152 // Skip elements not of the types in elp
153 if (!elp[this.tagName.toUpperCase()])
156 // Skip submit buttons and image elements
157 if ((this.type == 'submit') || (this.type == 'image'))
161 // Skip non-selected options
162 var sP = this.parentNode.nodeName.toUpperCase();
163 if (((sP == 'SELECT') || (sP == 'OPTGROUP')) && (!this.selected))
166 // Skip non-checked nodes
167 if (((this.type == 'radio') || (this.type == 'checkbox')) && (!this.checked))
170 // If we come here, everything is fine
171 var sN = this.name || this.id || this.parentNode.name || this.parentNode.id;
173 if ((!sN) && (sP == 'OPTGROUP'))
174 sN = this.parentNode.parentNode.name || this.parentNode.parentNode.id;
177 a.push({ name: sN, value: sV });
180 // Add submit button if needed
181 if (sButton && (sButton !== null)) {
182 var el = $(sButton).get(0);
184 name: el.name || el.id || el.parentNode.name || el.parentNode.id,
195 * example: $('someJQueryObject').update('sURL', 'sAction', 'aValues', 'fCallback');
196 * docs: Calls sURL with sAction and sends the aValues
197 * Puts the results from that call in the jQuery object and calls fCallback
199 $.fn.update = function(sURL, sMethod, aValues, fCallback) {
208 sResult = $.httpData(sResult);
210 // Update the element with the new HTML
213 // Evaluate the scripts AFTER this (so you can allready modify the new HTML!)
214 el.html(sResult).find("script").each(function(){
215 try { $.eval( this.text || this.textContent || this.innerHTML ); } catch(e) { }
218 // And call the callback handler :)
219 if (fCallback && (fCallback.constructor == Function))
226 * name: $.fn.serialize
227 * example: $('someJQueryObject').serialize('sButton', 'fCallback');
228 * docs: Calls the form's action with the correct method and the serialized values.
229 * Optionally adds the button which is clicked if you provide it.
230 * When there are results, the fCallback function is called.
232 $.fn.serialize = function(sButton, fCallback) {
233 var el = this.get(0);
239 $.param(this.formValues(sButton)),
241 if (fCallback && (fCallback.constructor == Function))
242 fCallback($.httpData(sResult));