Til sidst et stykke Javascript, der primært styrer modtagelse af sætninger.
Held og lykke. Du kan iøvrigt se en kørende version af chatten på
http://www.swat-crew.dk (denne version kræver dog at du logger ind).
Filnavn: chat.js
scrollBarRefreshRate = 100;
chatRefreshRate = 2000;
max_word_length = 20;
function chat_sendkey(control) {
key = event.keyCode;
if(key == 13) {
if(username != 'Anonymous') {
lineout = doEscape(document.clientform.textline.value);
if(lineout!='') {
lineout = '<span class=chat-nick>'+ username +'> </span>'+ lineout;
}
document.clientform.chatline.value = lineout;
document.clientform.textline.value = '';
top.chatframe.document.serverform.submit();
}
}
}
function chat_refresh() {
buffer = top.chatframe.document.serverform.chatdata.value;
lines = buffer.split('|');
out = '';
size = lines.length;
for(n=0;n<size;n++) {
out+= '<tr><td class="chat-line">'+ lines[n] +'</td></tr>';
}
out = '<table cellspacing=0 cellpadding=0 border=0>'+ out +'</table>';
linespan.innerHTML=out;
this.refreshScrollBar();
}
var chatRefreshId;
function chat_start() {
chatRefreshId = setInterval(refreshTimer, chatRefreshRate);
}
function chat_stop() {
clearInterval(chatRefreshId);
}
function refreshTimer() {
chat.refresh();
}
var scrollRefreshId;
function chat_refreshScrollBar() {
scrollRefreshId = setInterval(this.refreshScrollBarDelayed, scrollBarRefreshRate);
}
function chat_refreshScrollBarDelayed() {
linespan.scrollTop=10000;
clearInterval(scrollRefreshId);
}
function chatClass() {
this.refreshScrollBar = chat_refreshScrollBar;
this.refreshScrollBarDelayed = chat_refreshScrollBarDelayed;
this.refresh = chat_refresh;
this.start = chat_start;
this.stop = chat_stop;
this.sendkey = chat_sendkey;
}
function doEscape(string) {
words = string.split(' ');
error = false;
for(n=0;n<words.length;n++) {
if(words[n].length > max_word_length) {
alert('I am sorry, but this chat cannot handle words longer than '+ max_word_length +' characters.\n\nWhy don\'t you use our great IRC channel #swat-crew at Quakenet?');
return '';
}
}
string = string.replace(/\</g,'&lt;');
string = string.replace(/\"/g,'&quot;');
string = string.replace(/\'/g,'&#39;');
return string;
}