這是最關鍵、也是體量最大的一個檔案。我的習慣不太好,沒寫什麼詳細註解,就麻煩你仔細看了。
######### send.ASP ##############
<html>
<head>
<meta content="text/html; charset=gb2312" http-equiv="Content-Type">
<meta http-equiv='pragma' content='no-cache'>
<link rel="SHORTCUTICON" url="images/Bluenote.ico">
<style type="text/css">
td, body, select { font-family: "宋體"; font-size: 9pt }
.p9 { font-family:"宋體"; font-size:9pt; line-height:130%; width:68px; }
A { cursor:hand; text-decoration:none; font-family:"宋體"; font-size:9pt; }
input { font-size:9pt }
.an { background-color:FFFFFF; color:000000; border: 1 double }
</style>
<title>發言</title>
頁面一開頭先定義了整套「表情動作」語料。每則語料都是「動作描述|表情代號」的成對結構,用 $F 代表說話者、$T 代表對象,最後全部串接起來,再用 | 切分成陣列:
var tmp0="$F對$T說:|無表情|$F向$T微微一笑:|微笑|$F笑嘻嘻地對$T說:|嘻笑|..."
var tmp1="$F不解地問$T:|詢問|$F向$T吐了吐舌頭:|吐舌|$F非常自信地對$T說:|自信|..."
var tmp2="$F運足氣一聲斷喝:$T!|斷喝|$F幸災樂禍地對$T說:|幸災|$F露出無可奈何的表情:$T,|無奈|..."
var tmp3="$F向$T提出強烈抗議:|抗議|$F對$T大聲叫嚷:|叫嚷|$F目光盡赤,沖$T怒不可惡地狂吼:|暴怒|..."
var tmp4="$F手足無措地對$T說:|手足無措|$F得意洋洋地對$T說:|得意洋洋|..."
var etmp = tmp0 + tmp1 + tmp2 + tmp3 + tmp4;
var eV = etmp.split("|");
接著是兩個動態建構視窗文件的函數。wh() 負責寫出對話區的樣式與自動捲動腳本——它會檢查發言欄下方的「自動捲動」勾選框,決定要不要把視窗捲到底部:
function wh(fn){
fn.open();
fn.writeln("<!--★請另存為 Html 類型的文件★-->");
fn.writeln("<html><head><title>對話區</title>"
+ "<meta http-equiv=Content-Type content=\"text/html; charset=gb2312\">");
fn.writeln("<style type=text/css>.t{color:003366;font-size:9pt;}"
+ ".pic{font-size:17pt;}body{font-family:\"宋體\";font-size:10.5pt;"
+ "line-height:160%}A{text-decoration:none}"
+ "A:Hover{text-decoration:underline}A:visited{color:blue}</style>"
+ "</head>");
fn.writeln("<script language=\"JavaScript1.1\">");
fn.writeln("var autoScrollOn=1;");
fn.writeln("function scrollit(){"
+ "if(!parent.sd.oT.as.checked){autoScrollOn=0;return true;}"
+ "else{autoScrollOn=1;StartUp();return true;}}");
fn.writeln("function scrollWindow(){"
+ "if(autoScrollOn==1){this.scroll(0,65000);"
+ "setTimeout('scrollWindow()',200);}}");
fn.writeln("function scrollOn(){autoScrollOn=1;scrollWindow();}");
fn.writeln("function scrollOff(){autoScrollOn=0;}");
fn.writeln("function StartUp(){"
+ "parent.onblur=scrollOnFunction;parent.onfocus=scrollOffFunction;"
+ "scrollWindow();}");
fn.writeln("scrollOnFunction=new Function('scrollOn()');");
fn.writeln("scrollOffFunction=new Function('scrollOff()');");
fn.writeln("StartUp();</script>");
}
write() 則負責初始化兩個對話區:公開的「中心大廳」與每個人自己的「溫馨包廂」(只顯示和自己相關的發言):
function write(){
wh(parent.pub.document);
parent.pub.document.writeln("<body text=#000000 bgcolor=#eeeeff>");
parent.pub.document.writeln("<div id=\"div0\"></div>◆"
+ "<span style='color:#ff0088'>藍蝶聊天室</span>◆中心大廳<br>"
+ "輕輕敲醒沉睡的心靈,慢慢張開你的眼睛,"
+ "讓那昨日的憂傷隨聊而去!<hr size=1><div id=Ly></div>");
wh(parent.my.document);
var myd = parent.my.document;
myd.writeln("<body text=#000000 bgcolor=#eeeeff>");
myd.writeln("<div id=\"div0\"></div>◆"
+ "<span style='color:#ff0088'>藍蝶聊天室</span>◆溫馨包廂<br>");
myd.writeln("<span style='color:#0000ff;font-size:9pt'>"
+ "[這是您的個人包廂,只顯示和您相關的發言!]</span>"
+ "<hr size=1><div id=Ly></div>");
}
</script>
由於要處理中文,頁面還包含了兩個 VBScript 編碼工具函數。URLEncoding() 把中文字元逐字節轉成 %xx 形式;bytes2BSTR() 則把 XMLHTTP 回傳的位元組流還原成正確的雙字節中文:
<script language="vbscript">
Function URLEncoding(vstrIn)
strReturn = ""
For i = 1 To Len(vstrIn)
ThisChr = Mid(vstrIn, i, 1)
If Abs(Asc(ThisChr)) < &HFF Then
strReturn = strReturn & ThisChr
Else
innerCode = Asc(ThisChr)
If innerCode < 0 Then innerCode = innerCode + &H10000
Hight8 = (innerCode And &HFF00) \ &HFF
Low8 = innerCode And &HFF
strReturn = strReturn & "%" & Hex(Hight8) & "%" & Hex(Low8)
End If
Next
URLEncoding = strReturn
End Function
Function bytes2BSTR(vIn)
strReturn = ""
For i = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vIn, i, 1))
If ThisCharCode < &H80 Then
strReturn = strReturn & Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn, i + 1, 1))
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
i = i + 1
End If
Next
bytes2BSTR = strReturn
End Function
</script>
之後是發言資料物件與送出流程。oS() 定義了發言結構(發言人 F、對象 T、表情 E、內容 sC、顏色 cL),checksays() 做前端檢查並觸發送出,sendMsg() 才是真正走 XMLHTTP 的部分:
<script language="Javascript">
var Me = "<%=session("u_name")%>";
<% if session("u_sx")="1" then %>
var Msx = "girl.gif";
<% else %>
var Msx = "/School/UploadFiles_7810/201104/20110423155819991.gif";
<% end if %>
var Ts = new oS();
function oS(){
this.F=""; this.T=""; this.E=""; this.W="0";
this.sC=""; this.cL=""; this.Send = sendMsg;
}
function checksays(){
if (document.say.says.value == "") { alert("你要說什麼呢?"); return false; }
var fm = document.say;
var sC = fm.says.value;
var F = Me;
var T = fm.T.options[0].value;
var E = document.oT.userface.selectedIndex;
var cL = fm.says.style.color;
fm.says.value = "";
fm.says.focus();
Ts.F = F; Ts.T = T; Ts.E = E; Ts.sC = sC; Ts.cL = cL; Ts.W = "0";
setTimeout("Ts.Send();", 20);
return false;
}
function sendMsg(){
if (this.T == "" && this.F == "") { alert("你要對誰說?"); return; }
var sS = "F=" + code(this.F) + "&T=" + code(this.T)
+ "&sC=" + code(this.sC) + "&cL=" + this.cL
+ "&E=" + this.E;
sS = URLEncoding(sS);
var oSend = new ActiveXObject("Microsoft.XMLHTTP");
oSend.open("POST", "say.ASP", false);
oSend.setRequestHeader("Content-Length", sS.length);
oSend.setRequestHeader("CONTENT-TYPE", "application/x-www-form-urlencoded");
oSend.send(sS);
var oDom = new ActiveXObject("Microsoft.XMLDOM");
oDom.async = false;
body = bytes2BSTR(oSend.responseBody);
oDom.loadXML(body);
if (oDom.parseError.errorCode != 0) {
alert(body); alert("Sorry. 出現了錯誤"); return;
} else {
var aryF = oDom.documentElement.selectNodes("F");
var aryT = oDom.documentElement.selectNodes("T");
var arysC = oDom.documentElement.selectNodes("sC");
var arycL = oDom.documentElement.selectNodes("cL");
var arysJ = oDom.documentElement.selectNodes("sJ");
var aryE = oDom.documentElement.selectNodes("E");
var tmpHtml = "", img = "";
lengt = aryF.length;
for (var i = 0; i < lengt; i++) {
img = "<img src=/School/UploadFiles_7810/201104/"
+ "20110423155819991.gif width=12 height=12 border=0>";
tmpHtml = "<font style='cursor:hand;color:" + arycL(i).text + "'>"
+ document.oT.userface.options[aryE(i).text].value
+ rp2(arysC(i).text, "{^amp]", "&") + "</font> "
+ "<span class=t>[" + arysJ(i).text + "]</span><br>";
tmpHtml = rp2(tmpHtml, "$F",
"<a href=\"javascript:parent.sd.towho('"
+ aryF(i).text + "');\">" + img + aryF(i).text + "</a>");
tmpHtml = rp2(tmpHtml, "$T",
"<a href=\"javascript:parent.sd.towho('"
+ aryT(i).text + "');\">" + aryT(i).text + "</a>");
if (aryF(i).text == Me || aryT(i).text == Me)
parent.my.Ly.innerHTML += tmpHtml; // 進自己的包廂
else
parent.pub.Ly.innerHTML += tmpHtml; // 進公開大廳
}
}
}
function code(Str){
Str = Str.replace(/&/gi, "&")
Str = Str.replace(/</gi, "<")
Str = Str.replace(/>/gi, ">")
Str = Str.replace(/"/gi, "")
Str = Str.replace(/&/gi, "{^amp]")
return Str
}
function rp2(Str, Fs, Is){
while (Str.indexOf(Fs) != -1) Str = Str.replace(Fs, Is)
return Str
}
</script>
整體邏輯就是:前端收集發言與表情 → URL 編碼 → 同步 POST 到 say.ASP → 取回 XML → 用 bytes2BSTR 還原中文 → 依發言人是否為自己,分別 append 到「溫馨包廂」或「中心大廳」。整個過程頁面不刷新,這正是 XMLHTTP 的價值所在。