Andy's Blog » microajax, AJAX类, microajax js, AJAX类库, ajax库, this request send(this postBody);, 最小ajax, 最小 ajax 库, 最小 ajax, 最完美的ajax库 » 最小的AJAX类库 microajax

最小的AJAX类库 microajax

最小的AJAX类库
One of the smallest and easiest AJAX libraries

使用方法:

microAjax("/resource/url", function (res) {
 
alert (res);
});

服务端:

if ($_SERVER['X-Requested-With'] == 'XMLHttpRequest') {
 
// do something clever
}

下载地址: http://code.google.com/p/microajax/

源代码:

microajax.js
// microAjax by Stefan Lange-Hegermann
// this code is in the public domain
// you can do with it whatever you want!
 
function microAjax(url, callbackFunction)
{
    
this.bindFunction = function (caller, object) {
        
return function() {
            
return caller.apply(object, new Array(object));
        
}
    
}
 
    
this.stateChange = function (object) {
        
if (this.request.readyState==4) {
            
this.callbackFunction(this.request.responseText);
        
}
    
}
 
    
this.getRequest = function() {
        
if (window.ActiveXObject)
            
return new ActiveXObject('Microsoft.XMLHTTP');
        
else if (window.XMLHttpRequest)
            
return new XMLHttpRequest();
        
else
            
return false;
    
}
 
    
if (arguments[2])
        
this.postBody = arguments[2];
    
else
        
this.postBody="";
 
    
this.callbackFunction=callbackFunction;
    
this.url=url;   
    
this.request = this.getRequest();
 
    
if(this.request) {
        
this.request.onreadystatechange = this.bindFunction(this.stateChange, this);
 
        
if (this.postBody!="") {
            
this.request.open("POST", url, true);
            
this.request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
            
this.request.setRequestHeader('Connection', 'close');
        
} else {
            
this.request.open("GET", url, true);
        
}
 
        
this.request.send(this.postBody);
    
}
}

压缩版源代码:

microajax.minified.js
function microAjax(B,A){this.bindFunction=function(E,D){return function(){return E.apply(D,[D])}};this.stateChange=function(D){if(this.request.readyState==4){this.callbackFunction(this.request.responseText)}};this.getRequest=function(){if(window.ActiveXObject){return new ActiveXObject("Microsoft.XMLHTTP")}else{if(window.XMLHttpRequest){return new XMLHttpRequest()}}return false};this.postBody=(arguments[2]||"");this.callbackFunction=A;this.url=B;this.request=this.getRequest();if(this.request){var C=this.request;C.onreadystatechange=this.bindFunction(this.stateChange,this);if(this.postBody!==""){C.open("POST",B,true);C.setRequestHeader("X-Requested-With","XMLHttpRequest");C.setRequestHeader("Content-type","application/x-www-form-urlencoded");C.setRequestHeader("Connection","close")}else{C.open("GET",B,true)}C.send(this.postBody)}};

Incoming search terms:

Tags: Ajax, javascript, tiny

本文地址: http://www.21andy.com/new/20091221/1562.html

1 评论 to “PHP开源CMS之MODx”

  1. deeka 于 2009-12-21 08:38:56 发表:

    兼容现在主流的几个浏览器么?