jquery/ajax/ajax.js

90 lines
1.9 KiB
JavaScript
Raw Normal View History

2006-03-22 04:33:07 +01:00
// AJAX Plugin
// Docs Here:
// http://jquery.com/docs/ajax/
if ( typeof XMLHttpRequest == 'undefined' && typeof window.ActiveXObject == 'function') {
2006-03-23 22:13:20 +01:00
var XMLHttpRequest = function() {
return new ActiveXObject((navigator.userAgent.toLowerCase().indexOf('msie 5') >= 0) ?
"Microsoft.XMLHTTP" : "Msxml2.XMLHTTP");
};
2006-03-22 04:33:07 +01:00
}
$.xml = function( type, url, data, ret ) {
2006-03-23 22:13:20 +01:00
var xml = new XMLHttpRequest();
2006-03-22 04:33:07 +01:00
2006-03-23 22:13:20 +01:00
if ( xml ) {
xml.open(type || "GET", url, true);
2006-03-22 04:33:07 +01:00
2006-03-23 22:13:20 +01:00
if ( data )
xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
2006-03-22 04:33:07 +01:00
2006-03-23 22:13:20 +01:00
if ( ret )
xml.onreadystatechange = function() {
if ( xml.readyState == 4 ) ret(xml);
};
2006-03-22 04:33:07 +01:00
2006-03-23 22:13:20 +01:00
xml.send(data)
}
2006-03-22 04:33:07 +01:00
};
$.httpData = function(r,type) {
2006-03-23 22:13:20 +01:00
return r.getResponseHeader("content-type").indexOf("xml") > 0 || type == "xml" ?
r.responseXML : r.responseText;
2006-03-22 04:33:07 +01:00
};
$.get = function( url, ret, type ) {
2006-03-23 22:13:20 +01:00
$.xml( "GET", url, null, function(r) {
if ( ret ) ret( $.httpData(r,type) );
});
2006-03-22 04:33:07 +01:00
};
$.getXML = function( url, ret ) {
2006-03-23 22:13:20 +01:00
$.get( url, ret, "xml" );
2006-03-22 04:33:07 +01:00
};
$.post = function( url, data, ret, type ) {
2006-03-23 22:13:20 +01:00
$.xml( "POST", url, $.param(data), function(r) {
if ( ret ) ret( $.httpData(r,type) );
});
2006-03-22 04:33:07 +01:00
};
$.postXML = function( url, data, ret ) {
2006-03-23 22:13:20 +01:00
$.post( url, data, ret, "xml" );
2006-03-22 04:33:07 +01:00
};
$.param = function(a) {
2006-03-23 22:13:20 +01:00
var s = [];
for ( var i in a )
s[s.length] = i + "=" + encodeURIComponent( a[i] );
return s.join("&");
2006-03-22 04:33:07 +01:00
};
$.fn.load = function(a,o,f) {
2006-03-23 22:13:20 +01:00
// Arrrrghhhhhhhh!!
// I overwrote the event plugin's .load
// this won't happen again, I hope -John
if ( a && a.constructor == Function )
return this.bind("load", a);
2006-03-22 04:33:07 +01:00
2006-03-23 22:13:20 +01:00
var t = "GET";
if ( o && o.constructor == Function ) {
f = o;
o = null;
}
if (o != null) {
o = $.param(o);
t = "POST";
}
var self = this;
$.xml(t,a,o,function(h){
var h = h.responseText;
self.html(h).find("script").each(function(){
try {
eval( this.text || this.textContent || this.innerHTML );
} catch(e){}
});
if(f)f(h);
});
return this;
2006-03-22 04:33:07 +01:00
};