JSL Demo - Chatter

Implements a very simple chatting application in around 20 lines of code. Don't believe me? Take a look at the code. The JS code that powers this application is just 20 lines(24 to be exact(22 if you don't count the \n's - but I getting off track here)).

The Code...

var last_fetch = ""; // A global variable that holds the last chat status
function send(e) { //Called on a form submit
	JSL.event(e).stop(); // Stops the form submit event
	var msg = $("msg").value;
	if(!msg) return; //If the chat message is empty, don't send it
	$("msg").value = ""; // Clear the chat text box
	
	JSL.ajax("chat.php?action=send&msg="+msg).load(); //Send the chat message to the server - ajax call.
	JSL.dom("display").innerHTML = msg + "<br />" + JSL.dom("display").innerHTML; //Add the newly added message to the screen.
}
function fetch() { // Called every 3 seconds
	JSL.ajax("chat.php?action=fetch").load(function(txt){ //Ajax call that fetches the chat data as plain text.
		if(txt != last_fetch) {
			last_fetch = txt;
			JSL.dom("display").innerHTML = txt.replace(/\n/g, "<br />"); //And shows in on the screen.
		}
	});
}

function init() {
	$("msg").focus();
	
	JSL.dom("chat").on("submit", send); //Attaches the send() to the form 'chat' 's submit event.
	window.setInterval("fetch()", 3000); // Delay 3 seconds
}
JSL.dom(window).load(init); //Calls the init() on page load

See It In Action...

77
:O
is this for real
hello there
blah
Thiago Pereira (www.thiagopereira.cjb.net) Brasil
thiago
k
hdhgj
55555555
m
wasting rather...
so i'm waiting bandwidth by visiting this page, sweet!
dfdf
h
h
d
vv
rrr
eee
funny
how are you?
test
nmnbm
gambhiiram
kidilan

Comments

Anonymous at 19 Oct, 2008 10:10
how this chat is feed? is it thru a text file?
Reply to this.
Anonymous at 19 Oct, 2008 10:11
ok 20 lines but dont forget the javascript librarie wich feeds him.
Reply to this.
cssProdigy at 31 Dec, 2008 01:21
This is very nice and simple. BUT! To be a bit better chat client you should include a name next to the chat message so people can know who sent the message, but of course this is optional
Reply to this.
Anonymous at 26 Apr, 2009 12:52
nlnl
Reply to this.
Comment

Please dont enter you comments in this form - this is a fake form to confuse spamming bots. The next form is the real one.




Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.