/**
* FILE : HhlAjax.js
* VERSION : V2.0
* DESCRIPTION:
* this function return an ajax object : var ajx=getAjaxInstance("http://hhl_program.download.csdn.net/");
* PROPERTY:
* xmlhttp -- for saving a Object of XMLHttpRequest
* method -- "POST" or "GET"
* failed -- default value is false,if value is true that you get no ajax instance
* URL -- the url you want to access
* params -- parameters container
* response -- xmlhttp.responseText is the text value of the response
* responseXML -- xmlhttp.responseXML is the XML value of the response
*
*
*
*/
function getAjaxInstance(urlStr){
var __ajax=new Object();
__ajax.xmlhttp = null;
__ajax.method = "POST";
__ajax.URL = urlStr || null;
__ajax.failed = false;
__ajax.params = [];
__ajax.response = null;
__ajax.responseXML = null;
__ajax.add=function(name,value){
var param=new Object();
param.name=name;
param.value=value;
__ajax.params.push(param);
};
__ajax.callback = function(){
alert(" callback function /n You can Override like /n yourAjaxInstance.callback=function(){/*...*/};");
};
__ajax.clear = function(){
__ajax.params = [];
};
__ajax.create = function(){
try{
__ajax.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e1){
try{
__ajax.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e2){
__ajax.xmlhttp = null;
}
}
if (! __ajax.xmlhttp){
if (typeof XMLHttpRequest != "undefined"){
__ajax.xmlhttp = new XMLHttpRequest();
}
else{
__ajax.failed = true;
}
}
};
__ajax.onerror = function(){
alert(" Ajax Error : /n"+" status : "+__ajax.xmlhttp.status+"/n Error Text : "+__ajax.xmlhttp.statusText);
};
__ajax.seturl = function(urlString){
__ajax.URL = urlString || null;
};
__ajax.submit = function(){
if(__ajax.failed){
alert("Creat XMLHttpRequest failed!");
}
else if( ! __ajax.URL){
alert("Need the '__ajax.URL',which tell ajax instance to go where");
}
else{
//use param before clear
var queryString="?";
for(var i=0;i<__ajax.params.length;i++){
var param=__ajax.params[i];
queryString = queryString+param.name+"="+param.value+"&";
}
queryString = queryString.substring(0,queryString.length-1);
__ajax.xmlhttp.open(__ajax.method, __ajax.URL+queryString, true);
queryString = null;
__ajax.clear();
try{
__ajax.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
}catch(e){}
__ajax.xmlhttp.onreadystatechange = function(){
if(__ajax.xmlhttp.readyState==4){
__ajax.response = __ajax.xmlhttp.responseText;
__ajax.responseXML = __ajax.xmlhttp.responseXML;
if(__ajax.xmlhttp.status != 200){
__ajax.onerror();
}
else{
__ajax.callback();
}
}//end if(__ajax.xmlhttp.readyState==4){}
};
__ajax.xmlhttp.send();
}//end if(__ajax.failed){}else{}
};
__ajax.create();
return __ajax;
}// end function getAjaxInstance(){}
/** HhlAjax_test.htm *******/
<HTML>
<HEAD>
<SCRIPT type="text/javascript" SRC="HhlAjax.js"></SCRIPT>
<SCRIPT type="text/javascript">
function testAjax(){
ajx=getAjaxInstance("http://reg.163.com/login.jsp");
ajx.add("username","xxxx");
ajx.add("password","*****");
ajx.callback=function(){
document.write(ajx.response);
};
ajx.submit();
alert("continu access the same url 2nd");
ajx.add("username","vvvvv");
ajx.add("password","*****");
ajx.callback=function(){
document.write(ajx.response);
};
ajx.submit();
alert("continu access a different url");
ajx.seturl("http://www.google.com.hk/search");
ajx.add("q","ajax+responsexml");
ajx.method="GET";
ajx.callback=function(){
document.write(ajx.response);
};
ajx.submit();
}
testAjax();
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>