演示地址:
http://www.4yt.net/yc/jscallasp/testpage.htm
代码:
TestPage.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="application/xhtml-xml; charset=UTF-8" />
<meta http-equiv="Content-Language" content="zh-CN" />
<link rel="icon" href="/favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
<link rel="made" rev="made" href="mailto:griefforyou(at)gmail.com" />
<title>JS不刷新页面与ASP通信的测试</title>
<style type="text/css">
ol {margin-left:0px;}
ol li {margin-top:20px;}
</style>
<script type="text/javascript">
<!--
//Powerd by griefforyou
var Username;
function checkbydynamicscript(){
Username=document.getElementById("Username").value;
if(Username=="") {alert('请先输入用户名!');return}
//通过DOM动态创建一个Script元素,由ASP生成客户端脚本并执行
var script=document.createElement("script");
script.type="text/javascript";
script.src="CheckUser.asp?ReturnType=javascript&CallBack=CallBack&Username=" + Username;
document.getElementsByTagName("head")[0].appendChild(script);
}
//服务器返回JavaScript的回调函数
function CallBack(){
var rtnXML=CallBack.arguments[0];
showResult(rtnXML);
}
function checkbyiframe(){
Username=document.getElementById("Username").value;
if(Username=="") {alert('请先输入用户名!');return}
//尚未完成
}
function checkbyajax(){
Username=document.getElementById("Username").value;
if(Username=="") {alert('请先输入用户名!');return}
//AjAX其实就是 JavaScript+XMLHTTP
//创建XMLHTTP对象
var xmlhttp;
//for ie
try
{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
xmlhttp = null;
}
}
}
//for firefox
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
xmlhttp = new XMLHttpRequest();
}
if(xmlhttp==null) {alert('你的浏览器不支持XMLHTTP对象,无法采用AJAX技术!');return;}
xmlhttp.open("GET","CheckUser.asp?ReturnType=text&Username=" + Username,false);
xmlhttp.send(null);
var rtnText = xmlhttp.responseText;
showResult(rtnText);
xmlhttp=null;
}
function showResult(rtnText){
if(rtnText=='Exist') {alert('服务器返回信息:\n' + rtnText + '\n\n该用户名已经存在!');document.getElementById("Username").value='';}
else alert('服务器返回信息:\n' + rtnText + '\n\n该用户名可以正常使用!');
}
-->
</script>
</head>
<body>
<h3>JS不刷新页面与ASP通信的测试(griefforyou用户已经存在)</h3>
<form method="post" action="TestPage.htm">
<div>
<label>用户名:</label>
<input type="text" name="Username" id="Username" value="griefforyou" />
<label class="explain">用户名只能包含字母、数字及下划线。</label>
</div>
<div>
<label>口 令:</label>
<input type="text" name="Password" />
<label class="explain">建议您的口令包含字亩、数字及特殊符号。</label>
</div>
</form>
<ol>
<li>
<label>传统的iframe方法</label>
<input type="button" value="尚未完成" onclick="checkbyiframe()" disabled="disabled" />
</li>
<li>
<label>使用DOM动态插件Script元素的方法</label>
<input type="button" value="检测用户名" onclick="checkbydynamicscript()" />
</li>
<li>
<label>目前非常通行的AJAX技术</label>
<input type="button" value="检测用户名" onclick="checkbyajax()" />
</li>
</ol>
</body>
</html>
CheckUser.asp
<%
'Powerd by griefforyou
'检测用户名是否存在,并返回数据给客户端程序,返回的式可以用XML格或,纯文本格式,JS代码等等,看自己情况
Dim ReturnType,Username,CallBack
ReturnType=LCase(Request.QueryString("ReturnType"))
Username=Request.QueryString("Username")
CallBack=Request.QueryString("CallBack")
If ReturnType="text" Then
Response.AddHeader "Content-Type","text/plain"
ElseIf ReturnType="javascript" Then
Response.AddHeader "Content-Type","text/javascript"
Else
ReturnType="html"
Response.AddHeader "Content-Type","text/html"
End If
Response.Charset="UTF-8"
'这里我就不连接数据库了,假定 griefforyou 这个用户已经被注册
If ReturnType="text" Then
Dim OutPutText
If LCase(Username)="griefforyou" Then
OutPutText="Exist"
Else
OutPutText="Not Exist"
End If
Response.Write OutPutText
ElseIf ReturnType="javascript" Or ReturnType="html" Then
Dim OutPutJS
If LCase(Username)="griefforyou" Then
OutPutJS="Exist"
Else
OutPutJS="Not Exist"
End If
If ReturnType="javascript" And CallBack<>"" Then
Response.Write XMLToJS(OutPutJS) & vbCrlf & CallBack & "(resultXML)"
Else
Response.Write XMLToJS(OutPutJS)
End If
End If
Function XMLToJS(XMLSource)
Dim tempArr, i, XMLResult
XMLResult = Replace(Replace(Replace(XMLSource, "\", "\\"), """", "\"""), "'", "\'")
tempArr = Split(XMLResult, vbCrLf)
XMLResult = ""
For i = 0 To UBound(tempArr)
If i = 0 Then
XMLResult = XMLResult & "resultXML="
End If
XMLResult = XMLResult & """" & tempArr(i)
If i <> UBound(tempArr) Then
XMLResult = XMLResult & "\n""+" & vbCrLf
Else
XMLResult = XMLResult & """" & vbCrLf
End If
Next
XMLToJS = XMLResult
End Function
%>