AjaxFun.js (调用函数)
function
createXMLHttpRequest()
{
try
{
xmlHttp = new XMLHttpRequest();
return xmlHttp;
}
catch (trymicrosoft)
{
try
{
xmlHttp = new ActiveXObject( " Msxml2.XMLHTTP " );
return xmlHttp;
}
catch (othermicrosoft)
{
try
{
xmlHttp = new ActiveXObject( " Microsoft.XMLHTTP " );
return xmlHttp;
}
catch (failed)
{
return xmlHttp;
}
}
}
if ( ! xmlHttp)
{
return false ;
}
}
function getResponseText(method,url,obj)
{
xmlHttp = createXMLHttpRequest();
if (xmlHttp == null )
{
return " [error] " ;
}
xmlHttp.open(method,url);
xmlHttp.onreadystatechange = function ()
{
if ( 4 == xmlHttp.readyState)
{
if ( 200 == xmlHttp.status)
{
obj.innerHTML = xmlHttp.responseText;
}
}
}
xmlHttp.send();
}
{
try
{
xmlHttp = new XMLHttpRequest();
return xmlHttp;
}
catch (trymicrosoft)
{
try
{
xmlHttp = new ActiveXObject( " Msxml2.XMLHTTP " );
return xmlHttp;
}
catch (othermicrosoft)
{
try
{
xmlHttp = new ActiveXObject( " Microsoft.XMLHTTP " );
return xmlHttp;
}
catch (failed)
{
return xmlHttp;
}
}
}
if ( ! xmlHttp)
{
return false ;
}
}
function getResponseText(method,url,obj)
{
xmlHttp = createXMLHttpRequest();
if (xmlHttp == null )
{
return " [error] " ;
}
xmlHttp.open(method,url);
xmlHttp.onreadystatechange = function ()
{
if ( 4 == xmlHttp.readyState)
{
if ( 200 == xmlHttp.status)
{
obj.innerHTML = xmlHttp.responseText;
}
}
}
xmlHttp.send();
}
test.html
<
script
language
=javascript
src
="AjaxFun.js"
></
script
>
< input type ="text" name ="UserName" size ="20" >
< div id ="UserNameHint" ></ div >
< script language ="javascript" for ="UserName" event ="onblur" >
obj = document.getElementById( " UserName " );
objHint = document.getElementById( " UserNameHint " );
getResponseText( ' get ' , ' test.asp?username= ' + obj.value,objHint)
</ script >
< input type ="text" name ="UserName" size ="20" >
< div id ="UserNameHint" ></ div >
< script language ="javascript" for ="UserName" event ="onblur" >
obj = document.getElementById( " UserName " );
objHint = document.getElementById( " UserNameHint " );
getResponseText( ' get ' , ' test.asp?username= ' + obj.value,objHint)
</ script >
test.asp 是用来检证的,和我们常用的asp语法一样 request.querystring就可以了。