forgot to remove the old serialize function
[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 $.param = function(a) {
94         var s = [];
95         if (a && typeof a == 'object' && a.constructor == Array) {
96                 for ( var i=0; i < a.length; i++ )
97                         s[s.length] = a[i]['name'] + "=" + encodeURIComponent( a[i]['value'] );
98         } else {
99                 for ( var i in a )
100                         s[s.length] = i + "=" + encodeURIComponent( a[i] );
101         }
102         return s.join("&");
103 };
104
105 $.fn.load = function(a,o,f) {
106         // Arrrrghhhhhhhh!!
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);
111
112         var t = "GET";
113         if ( o && o.constructor == Function ) {
114                 f = o;
115                 o = null;
116         }
117         if (o != null) {
118                 o = $.param(o);
119                 t = "POST";
120         }
121         var self = this;
122         $.xml(t,a,o,function(h){
123                 var h = h.responseText;
124                 self.html(h).find("script").each(function(){
125                         try {
126                                 eval( this.text || this.textContent || this.innerHTML );
127                         } catch(e){}
128                 });
129                 if(f)f(h);
130         });
131         return this;
132 };
133
134 /**
135  * function:    $.fn.formValues
136  * usage:               $('#frmLogin').formValues()
137  * docs:                        Gets the form values and creates a key=>value array of the found values (only for ENABLED elements!)
138  */
139 $.fn.formValues = function() {
140         var a = new Array();
141         this.find("input[@type='submit'],input[@type='hidden'],textarea,input[@checked],input[@type='password'],input[@type='text'],option[@selected]")
142                 .filter(":enabled").each(function() {
143                         o = {};
144                         o['name'] = this.name || this.id || this.parentNode.name || this.parentNode.id;
145                         o['value'] = this.value;
146                         a.push(o);
147                 });
148         return a;
149 };
150
151 /**
152  * function:    $.update
153  * usage:               $.update('someJQueryObject', 'someurl', 'array');
154  * docs:                        Mimics the ajaxUpdater from prototype. Posts the key=>value array to the url and
155  *                                      puts the results from that call in the jQuery object specified.
156  *                                      --> If you set the blnNoEval to true, the script tags are NOT evaluated.
157  */
158 $.update = function(objElement, strURL, arrValues, fncCallback) {
159         $.post(strURL, arrValues, function(strHTML) {
160                 //
161                 // Update the element with the new HTML
162                 objElement.html(strHTML);
163
164                 //
165                 // Evaluate the scripts
166                 objElement.html(strHTML).find("script").each(function(){
167                         try { eval( this.text || this.textContent || this.innerHTML ); } catch(e){}
168                 });
169
170                 //
171                 // Callback handler
172                 if (fncCallback) fncCallback();
173         });
174 };