Added support for global AJAX callbacks and Form Serialization.
[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 $.xml = function( type, url, data, ret ) {
13         var xml = new XMLHttpRequest();
14
15         if ( xml ) {
16                 xml.open(type || "GET", url, true);
17
18                 if ( data )
19                         xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
20
21                 xml.onreadystatechange = function() {
22                         if ( xml.readyState == 4 ) {
23                                 if ( ret ) ret(xml);
24                                 $.triggerAJAX( $.httpData(xml) );
25                         }
26                 };
27
28                 xml.send(data)
29         }
30 };
31
32 $.httpData = function(r,type) {
33         return r.getResponseHeader("content-type").indexOf("xml") > 0 || type == "xml" ?
34                 r.responseXML : r.responseText;
35 };
36
37 $.get = function( url, ret, type ) {
38         $.xml( "GET", url, null, function(r) {
39                 if ( ret ) ret( $.httpData(r,type) );
40         });
41 };
42
43 $.getXML = function( url, ret ) {
44         $.get( url, ret, "xml" );
45 };
46
47 $.post = function( url, data, ret, type ) {
48         $.xml( "POST", url, $.param(data), function(r) {
49                 if ( ret ) ret( $.httpData(r,type) );
50         });
51 };
52
53 $.postXML = function( url, data, ret ) {
54         $.post( url, data, ret, "xml" );
55 };
56
57 // Global AJAX Event Binding
58 // Requested here:
59 // http://jquery.com/discuss/2006-March/000415/
60
61 $.fn.handleAJAX = function( callback ) {
62         $.ajaxHandles = $.merge( $.ajaxHandles, this.cur );
63         return this.bind( 'ajax', callback );
64 };
65
66 $.ajaxHandles = [];
67 $.triggerAJAX = function(data){
68         for ( var i = 0; i < $.ajaxHandles.length; i++ )
69                 triggerEvent( $.ajaxHandles[i], 'ajax', [data] );
70 };
71
72 // Dynamic Form Submission
73 // Based upon the mailing list post at:
74 // http://jquery.com/discuss/2006-March/000424/
75
76 $.fn.serialize = function(callback) {
77         return this.each(function(){
78                 var a = {};
79                 $(this)
80                         .find("input:checked,hidden,text,option[@selected],textarea")
81                         .filter(":enabled").each(function() {
82                                 a[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value;
83                         });
84                 $.xml( this.method || "GET", this.action || "", $.param(a), callback );
85         });
86 };
87
88 $.param = function(a) {
89         var s = [];
90         for ( var i in a )
91         s[s.length] = i + "=" + encodeURIComponent( a[i] );
92         return s.join("&");
93 };
94
95 $.fn.load = function(a,o,f) {
96         // Arrrrghhhhhhhh!!
97         // I overwrote the event plugin's .load
98         // this won't happen again, I hope -John
99         if ( a && a.constructor == Function )
100                 return this.bind("load", a);
101
102         var t = "GET";
103         if ( o && o.constructor == Function ) {
104                 f = o;
105                 o = null;
106         }
107         if (o != null) {
108                 o = $.param(o);
109                 t = "POST";
110         }
111         var self = this;
112         $.xml(t,a,o,function(h){
113                 var h = h.responseText;
114                 self.html(h).find("script").each(function(){
115                         try {
116                                 eval( this.text || this.textContent || this.innerHTML );
117                         } catch(e){}
118                 });
119                 if(f)f(h);
120         });
121         return this;
122 };