开发者

JavaScript chat room username colours

I am working on an AJAX/JavaScript/PHP chat room and I need a bit of advice. Basically, when the values are sent (message (txt_message), name and chat id (chat)) like so:

var param = 'message=' + document.getElementById('txt_message').value;
param += '&name=<b><?php echo $_SESSION['username']; ?></b>';
param += '&chat=2';
sendReq.send(param);
document.getElementB开发者_如何转开发yId('txt_message').value = '';

I want to make it so each instance of a username ($_SESSION['username']) is a different colour automatically which stays at that particular colour for the duration of the chat. As you will see, so far I have just got the username in bold; I dont know if I am trying to put code in the wrong place or it should be somewhere else. These values are put into a div and i can edit the styles there, but it changes all the just not just the username. Thanks in advance for any advice i recieve and apologies if my explanation is a bit confusing :)


you can use a hash-function over the username to assign a color to an username. i.e. use 16 colors and take the first char of the md5sum, so you will assign (mostly) different colors, and one username will always have the same color.


You could do something client side

By looping through the chat room attendees and applying a css style/class to the which has a color set using a hex which is incremented as you run through the loop

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜