前段时间做项目使用了prototype.js后来发现一些复杂的情况,有时会有内存溢出的情况,索性干脆不用了,为了保证程序的兼容性,按prototype的接口实现了一个简单的ajax封装,火狐下没有测试.代码没格式化主要是想压缩行数.
<
html
>
< head >
< script >
var syj = {};
// 发送ajax请求的方法
syj.Ajax = function (url,obj){
var httpRequest;
if (window.ActiveXObject) try {httpRequest = new ActiveXObject( " Microsoft.XMLHTTP " );} catch (e){ try {httpRequest = new ActiveXObject( " Msxml2.XMLHTTP " );} catch (e){}}
if ( ! httpRequest){alert( ' 不能创建XMLHTTP实例 ' );obj.onComplete();}
httpRequest.onreadystatechange = function (){ if (httpRequest.readyState == 4 ){obj[ ' onComplete ' ](httpRequest);}}
if (url.indexOf( ' machineDate ' ) ==- 1 ) url += (url.indexOf( ' ? ' ) ==- 1 ? " ? " : " & " ) + ( " machineDate= " + new Date().getTime());
url += (url.indexOf( ' ? ' ) ==- 1 ? " ? " : " & " ) + obj.parameters;
if (obj.asynchronous == true ) httpRequest.open(obj.method, url); else httpRequest.open(obj.method, url, false );
httpRequest.send( null );
return httpRequest;
};
function test(){
alert( " test start " );
var url = " http://www.baidu.com/ " ;
syj.Ajax(url,{
method : " GET " ,
parameters : " s?wd=csdn " ,
asynchronous : true ,
onFailure : function (httpRequest){
alert( " 出错了! " );
},
onComplete : function (httpRequest){
if (httpRequest.status == 200 ){
alert(httpRequest.responseText);
}
}
}
);
alert( " test end " );
}
</ script >
</ head >
< body >
< input type ="button" onclick ="test()" value ="测试" />
</ body >
</ html >
< head >
< script >
var syj = {};
// 发送ajax请求的方法
syj.Ajax = function (url,obj){
var httpRequest;
if (window.ActiveXObject) try {httpRequest = new ActiveXObject( " Microsoft.XMLHTTP " );} catch (e){ try {httpRequest = new ActiveXObject( " Msxml2.XMLHTTP " );} catch (e){}}
if ( ! httpRequest){alert( ' 不能创建XMLHTTP实例 ' );obj.onComplete();}
httpRequest.onreadystatechange = function (){ if (httpRequest.readyState == 4 ){obj[ ' onComplete ' ](httpRequest);}}
if (url.indexOf( ' machineDate ' ) ==- 1 ) url += (url.indexOf( ' ? ' ) ==- 1 ? " ? " : " & " ) + ( " machineDate= " + new Date().getTime());
url += (url.indexOf( ' ? ' ) ==- 1 ? " ? " : " & " ) + obj.parameters;
if (obj.asynchronous == true ) httpRequest.open(obj.method, url); else httpRequest.open(obj.method, url, false );
httpRequest.send( null );
return httpRequest;
};
function test(){
alert( " test start " );
var url = " http://www.baidu.com/ " ;
syj.Ajax(url,{
method : " GET " ,
parameters : " s?wd=csdn " ,
asynchronous : true ,
onFailure : function (httpRequest){
alert( " 出错了! " );
},
onComplete : function (httpRequest){
if (httpRequest.status == 200 ){
alert(httpRequest.responseText);
}
}
}
);
alert( " test end " );
}
</ script >
</ head >
< body >
< input type ="button" onclick ="test()" value ="测试" />
</ body >
</ html >