温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:Ajax实现的在线聊天室
当前文件:
AjaxChat/js/AjaxFunctions.js[3K,2009-6-12 11:31:35],打开代码结构图
AjaxChat/js/AjaxFunctions.js[3K,2009-6-12 11:31:35],打开代码结构图1//Global variables 2
var timeID; 3
var refreshRate = 2000; // two seconds 4
var rnd = Math.random(); 5
6
var isFirefox; 7
var isIE; 8
9
//var XmlHttp; 10
var AjaxServerPageName; 11
AjaxServerPageName = "Server.aspx"; 12
13
//Creating and setting the instance of appropriate XMLHTTP Request object to a 揦mlHttp?variable 14
function getAjax() 15
{ 16
var XmlHttp; 17
18
//Creating object of XMLHTTP in IE 19
try 20
{ 21
XmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 22
} 23
catch(e) 24
{ 25
try 26
{ 27
XmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 28
} 29
catch(oc) 30
{ 31
XmlHttp = null; 32
} 33
} 34
//Creating object of XMLHTTP in Mozilla and Safari 35
if(!XmlHttp && typeof XMLHttpRequest != "undefined") 36
{ 37
XmlHttp = new XMLHttpRequest(); 38
} 39
return XmlHttp; 40
} 41
42
// Get browser type 43
function sniffBrowserType() { 44
isFirefox = ( navigator.appName == "Netscape" ); 45
isIE = (navigator.appName == "Microsoft Internet Explorer" ); 46
} 47
48
49
// Capture the enter key on the input box and post message 50
function captureReturn( event ) 51
{ 52
if(event.which || event.keyCode) 53
{ 54
if ((event.which == 13) || (event.keyCode == 13)) 55
{ 56
postText(); 57
return false; 58
} 59
else { 60
return true; 61
} 62
} 63
} 64
65
// Start the update timer 66
function setTimers() 67
{ 68
timeID = window.setTimeout( "updateAll()", refreshRate ); 69
} 70
71
// Start to update and reset the update timer 72
function updateAll() 73
{ 74
window.clearTimeout( timeID ); 75
getUserList(); 76
setTimers(); 77
} 78
79
function getUserList() 80
{ 81
rnd++; 82
url = 'Server.aspx?action=UserList&session=' + rnd; 83
req = getAjax(); 84
85
req.onreadystatechange = function(){ 86
87
if( req.readyState == 4 && req.status == 200 ) { 88
89
obj = getElement( "userlist" ); 90
obj.innerHTML = req.responseText; 91
getBufferText(); 92
} 93
94
} 95
96
req.open( 'GET', url, true ); 97
req.send( null ); 98
} 99
100
function getBufferText() 101
{ 102
rnd++; 103
url = 'Server.aspx?action=GetMsg&session=' + rnd; 104
req = getAjax(); 105
106
req.onreadystatechange = function(){ 107
108
if( req.readyState == 4 && req.status == 200 ) { 109
110
obj = getElement( "chatbuffer" ); 111
obj.innerHTML = req.responseText; 112
scrollChatPane(); 113
//FocusWindow(); 114
} 115
} 116
117
req.open( 'GET', url , true ); 118
req.send( null ); 119
} 120
121
function postText() 122
{ 123
rnd++; 124
chatbox = getElement( "mytext" ); 125
chat = chatbox.value; 126
chatbox.value = ""; 127
128
userid = location.search.substring( 1, location.search.length ); 129
url = 'Server.aspx?action=PostMsg&u=' + userid + '&t=' + encodeURIComponent(chat) + '&session=' + rnd; 130
131
req = getAjax(); 132
133
req.onreadystatechange = function(){ 134
135
if( req.readyState == 4 && req.status == 200 ) { 136
updateAll(); 137
} 138
139
} 140
141
req.open( 'GET', url, true ); 142
req.send( null ); 143
} 144
145
function getElement( id ) 146
{ 147
if( isIE ) { 148
return document.all[ id ]; 149
} 150
else { 151
return document.getElementById( id ); 152
} 153
} 154
155
function showLoadScreen() 156
{ 157
var loading = "<div style=\"text-align:center;color:red;\"><h5>Loading...</h5></div>"; 158
159
chat = getElement( "chatbuffer" ); 160
chat.innerHTML = loading; 161
162
user = getElement( "userlist" ); 163
user.innerHTML = loading; 164
} 165
166
function scrollChatPane() 167
{ 168
var obj = document.getElementById("chatpane"); 169
obj.scrollTop = obj.scrollHeight; 170
} 171
172
function setFocus(ControlName) 173
{ 174
var control = document.getElementById(ControlName); 175
if( control != null ) 176
{ 177
control.focus(); 178
} 179
} 180
function FocusWindow() 181
{ 182
183
//TODO: FireFox doesn't work? 184
window.focus(); 185
186
}






}
}