DIV CSS 佈局教程網

AJAX 入門聊天室完整程式碼
編輯:AJAX基礎知識     

早就想把這份程式碼分享出來,一直抽不出時間,今天趁空先把程式碼放上來,註解明天再補。

還那句老話:Ajax 是一種應用,而非某項專門的技術。做 DEMO 講求的是速度,也要讓看的人容易理解。JS 是最基本的 Web 語言,比起其他語言要親切得多,所以我仍選擇用 JS 撰寫前後台程式。這並不代表我不懂其他語言——程式的關鍵在於你對「如何實現」的想法,用什麼語言就像選工具,菜刀能做、瑞士軍刀也能做,重點是看場合。

就 Ajax 而言,首要考量其實是人性化:順暢的人機互動才是它真正的優勢,否則還不如用 Flash 的 AS 來得更快。請記住一個詞——「認知潛意識」:千萬別讓使用者感到操作上的不適,而且要總是走在使用者前面,這或許正是程式「不夠智能」的癥結所在。

如果你喜歡,歡迎在這份基礎上繼續開發;若願意註明轉載出處,我會非常開心,並寫出更多更好的內容。

首頁 HTML(請另存為 index.html)

程式碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Chating Beta_0.3.23</title>
<link href="images/chatstyle.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="images/chat.js"></script>
<script type="text/javascript" src="images/display.js"></script>
</head>
<body>
<div id="loadifo"></div>
<div class="header">Chating Beta_0.3.23</div>
<div id="outs"><div id="outmain"><div id="chatmain"></div></div></div>
<div class="in">昵稱:
    <input class="names" name="named" type="text" />
    <span id="inputput" class="inputput" onmouseover="overInput(this)" onmouseout="outInput(this)"><input class="inputs" name="inputs" type="text" title="提交對話 快捷鍵:Enter或Ctrl + Enter 清除 快捷鍵:Ctrl + Delete" /><input class="left" type="button" onmouseover="lrover(this)" value=" " onmouseout="lrout(this)" onclick="preChat();" title="向前一條對話記錄 快捷鍵:↑" /><input class="right" onmouseover="lrover(this)" type="button" value=" " onmouseout="lrout(this)" onclick="nextChat();" title="向後一條對話記錄 快捷鍵:↓" />
    <input id="btn" class="btn" type="image" src="images/rest.gif" onmouseover="overBtn(this)" onmouseout="outBtn(this)" onclick="doUsingPost();" title="提交對話 快捷鍵:Enter或Ctrl + Enter" />
    </span>
    <span id="errors"></span>
</div>
</body>
</html>

CSS 檔(請另存為 images/chatstyle.css)

程式碼

body{ margin:0px; padding:0px; font-size:9pt; background-image:url(JD112.jpg); background-position:center center; }
#outmain{ height:400px; font-size:9pt; overflow-y:scroll; overflow-x:hidden; }
#outs{ width:540px; margin-right:auto; margin-left:auto; padding:10px; margin-top:10px; margin-bottom:10px; background-color:#FFF9E1; border:1px solid #AAA; clear:both; }
.in{ text-align:center; }
.names{ border:1px solid #AAA; background-color:#FFF9E1; width:80px; height:19px; line-height:19px; }
#loadifo{ position:absolute; top:100px; z-index:1; right:10px; line-height:21px; }
.header{ height:60px; background-color:#000; text-align:center; color:#FFF; font-weight:bold; line-height:60px; font-family:Tahoma; font-size:12pt; float:left; width:100%; margin-bottom:20px; filter:Alpha(Opacity=50); opacity:0.5; }
.btn{ border:1px solid #AAA; position:absolute; margin-top:2px; }
.inputs{ font-size:9pt; background-image:url(input.png); background-repeat:no-repeat; width:295px; line-height:21px; height:21px; margin:0px; padding:0px; border:1px solid #AAA; }
.left,.right{ background-repeat:no-repeat; background-position:center center; cursor:pointer; background-color:#FFF; height:23px; width:23px; }
.left{ background-image:url(l1.gif); }
.right{ background-image:url(r1.gif); }

主 JS 檔(請另存為 images/chat.js)

程式碼

此檔負責建立跨瀏覽器的 XMLHTTP 物件、以 GET 輪詢抓取訊息(setInterval 每秒呼叫 doUsingGet)、以 POST 送出對話,並解析回傳的 XML、將訊息附加到聊天區。核心函式包含 createxmldoc、doUsingGet、doUsingPost、CheckState、resXml、getTag 等;同時處理鍵盤快捷鍵(Enter/Ctrl+Enter 送出、上下鍵瀏覽歷史紀錄、Ctrl+Delete 清除)。

輔助 JS 檔(請另存為 images/display.js)

程式碼

負責輸入框與按鈕的滑鼠移入移出效果、焦點控制,以及對話歷史紀錄的addChat / preChat / nextChat 邏輯(最多保留 199 筆)。

主 ASP 程式(請另存為 chat.asp)

程式碼

以 JScript 撰寫,透過 Application 物件在記憶體中保存最近 21 筆訊息(names、msgs、ips),並依 client 的目前編號回傳尚未取得的項目,輸出為 gb2312 編碼的 XML。收到 cont=cls 時則清空 Application。

全部程式已打包,點擊即可下載。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved