Added my bugfixes and removed the triggerAjax and handleAjax functions since they...
[jquery.git] / ajax / ajax.js
1 // AJAX Plugin
2 // Docs Here:
3 // http://jquery.com/docs/ajax/
4
5 if ( typeof XMLHttpRequest == 'undefined' && typeof window.ActiveXObject == 'function') {
6         var XMLHttpRequest = function() {
7                 return new ActiveXObject((navigator.userAgent.toLowerCase().indexOf('msie 5') >= 0) ?
8                         "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP");
9         };
10 }
11
12 //
13 // Counter for holding the active query's
14 $.xmlActive=0;
15
16 $.xml = function( type, url, data, ret ) {
17         var xml = new XMLHttpRequest();
18
19         if ( xml ) {
20                 //
21                 // Increase the query counter
22                 $.xmlActive++;
23
24                 //
25                 // Show loader if needed
26                 if ($.xmlCreate)
27                         $.xmlCreate();
28
29                 //
30                 // Open the socket
31                 xml.open(type || "GET", url, true);
32
33                 if ( data )
34                         xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
35
36                 //
37                 // Set header so calling script knows that it's an XMLHttpRequest
38                 xml.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
39
40                 /* Force "Connection: close" for Mozilla browsers to work around
41                  * a bug where XMLHttpReqeuest sends an incorrect Content-length
42                  * header. See Mozilla Bugzilla #246651.
43                  */
44                 if ( xml.overrideMimeType )
45                         xml.setRequestHeader('Connection', 'close');
46
47                 xml.onreadystatechange = function() {
48                         if ( xml.readyState == 4 ) {
49                                 if ( ret ) ret(xml);
50
51                                 //
52                                 // Decrease counter
53                                 $.xmlActive--;
54
55                                 //
56                                 // Hide loader if needed
57                                 if ($.xmlActive <= 0) {
58                                         if ($.xmlDestroy)
59                                                 $.xmlDestroy();
60                                 }
61                         }
62                 };
63
64                 xml.send(data)
65         }
66 };
67
68 $.httpData = function(r,type) {
69         return r.getResponseHeader("content-type").indexOf("xml") > 0 || type == "xml" ?
70                 r.responseXML : r.responseText;
71 };
72
73 $.get = function( url, ret, type ) {
74         $.xml( "GET", url, null, function(r) {
75                 if ( ret ) ret( $.httpData(r,type) );
76         });
77 };
78
79 $.getXML = function( url, ret ) {
80         $.get( url, ret, "xml" );
81 };
82
83 $.post = function( url, data, ret, type ) {
84         $.xml( "POST", url, $.param(data), function(r) {
85                 if ( ret ) ret( $.httpData(r,type) );
86         });
87 };
88
89 $.postXML = function( url, data, ret ) {
90         $.post( url, data, ret, "xml" );
91 };
92
93 // Dynamic Form Submission
94 // Based upon the mailing list post at:
95 // http://jquery.com/discuss/2006-March/000424/
96
97 $.fn.serialize = function(callback) {
98         return this.each(function(){
99                 var a = {};
100                 $(this)
101                         .find("input:checked,hidden,text,option[@selected],textarea")
102                         .filter(":enabled").each(function() {
103                                 a[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value;
104                         });
105                 $.xml( this.method || "GET", this.action || "", $.param(a), callback );
106         });
107 };
108
109 $.param = function(a) {
110         var s = [];
111         if (a && typeof a == 'object' && a.constructor == Array) {
112                 for ( var i=0; i < a.length; i++ )
113                         s[s.length] = a[i]['name'] + "=" + encodeURIComponent( a[i]['value'] );
114         } else {
115                 for ( var i in a )
116                         s[s.length] = i + "=" + encodeURIComponent( a[i] );
117         }
118         return s.join("&");
119 };
120
121 $.fn.load = function(a,o,f) {
122         // Arrrrghhhhhhhh!!
123         // I overwrote the event plugin's .load
124         // this won't happen again, I hope -John
125         if ( a && a.constructor == Function )
126                 return this.bind("load", a);
127
128         var t = "GET";
129         if ( o && o.constructor == Function ) {
130                 f = o;
131                 o = null;
132         }
133         if (o != null) {
134                 o = $.param(o);
135                 t = "POST";
136         }
137         var self = this;
138         $.xml(t,a,o,function(h){
139                 var h = h.responseText;
140                 self.html(h).find("script").each(function(){
141                         try {
142                                 eval( this.text || this.textContent || this.innerHTML );
143                         } catch(e){}
144                 });
145                 if(f)f(h);
146         });
147         return this;
148 };
149
150 /**
151  * function:    $.fn.formValues
152  * usage:               $('#frmLogin').formValues()
153  * docs:                        Gets the form values and creates a key=>value array of the found values (only for ENABLED elements!)
154  */
155 $.fn.formValues = function() {
156         var a = new Array();
157         this.find("input[@type='submit'],input[@type='hidden'],textarea,input[@checked],input[@type='password'],input[@type='text'],option[@selected]")
158                 .filter(":enabled").each(function() {
159                         o = {};
160                         o['name'] = this.name || this.id || this.parentNode.name || this.parentNode.id;
161                         o['value'] = this.value;
162                         a.push(o);
163                 });
164         return a;
165 };
166
167 /**
168  * function:    $.update
169  * usage:               $.update('someJQueryObject', 'someurl', 'array');
170  * docs:                        Mimics the ajaxUpdater from prototype. Posts the key=>value array to the url and
171  *                                      puts the results from that call in the jQuery object specified.
172  *                                      --> If you set the blnNoEval to true, the script tags are NOT evaluated.
173  */
174 $.update = function(objElement, strURL, arrValues, fncCallback) {
175         $.post(strURL, arrValues, function(strHTML) {
176                 //
177                 // Update the element with the new HTML
178                 objElement.html(strHTML);
179
180                 //
181                 // Evaluate the scripts
182                 objElement.html(strHTML).find("script").each(function(){
183                         try { eval( this.text || this.textContent || this.innerHTML ); } catch(e){}
184                 });
185
186                 //
187                 // Callback handler
188                 if (fncCallback) fncCallback();
189         });
190 };