Fixed more formatting/tab problems.
[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                 if ( ret )
22                         xml.onreadystatechange = function() {
23                                 if ( xml.readyState == 4 ) ret(xml);
24                         };
25
26                 xml.send(data)
27         }
28 };
29
30 $.httpData = function(r,type) {
31         return r.getResponseHeader("content-type").indexOf("xml") > 0 || type == "xml" ?
32                 r.responseXML : r.responseText;
33 };
34
35 $.get = function( url, ret, type ) {
36         $.xml( "GET", url, null, function(r) {
37                 if ( ret ) ret( $.httpData(r,type) );
38         });
39 };
40
41 $.getXML = function( url, ret ) {
42         $.get( url, ret, "xml" );
43 };
44
45 $.post = function( url, data, ret, type ) {
46         $.xml( "POST", url, $.param(data), function(r) {
47                 if ( ret ) ret( $.httpData(r,type) );
48         });
49 };
50
51 $.postXML = function( url, data, ret ) {
52         $.post( url, data, ret, "xml" );
53 };
54
55 $.param = function(a) {
56         var s = [];
57         for ( var i in a )
58         s[s.length] = i + "=" + encodeURIComponent( a[i] );
59         return s.join("&");
60 };
61
62 $.fn.load = function(a,o,f) {
63         // Arrrrghhhhhhhh!!
64         // I overwrote the event plugin's .load
65         // this won't happen again, I hope -John
66         if ( a && a.constructor == Function )
67                 return this.bind("load", a);
68
69         var t = "GET";
70         if ( o && o.constructor == Function ) {
71                 f = o;
72                 o = null;
73         }
74         if (o != null) {
75                 o = $.param(o);
76                 t = "POST";
77         }
78         var self = this;
79         $.xml(t,a,o,function(h){
80                 var h = h.responseText;
81                 self.html(h).find("script").each(function(){
82                         try {
83                                 eval( this.text || this.textContent || this.innerHTML );
84                         } catch(e){}
85                 });
86                 if(f)f(h);
87         });
88         return this;
89 };