开发者

How to make notification on message send like Facebook

How a notification can be shown after we are receiving a new message like FACEBOOK is displaying without r开发者_JAVA技巧efreshing a page. ( facebook is showing RED color notification after receiving a new message).

Thank You


There are two ways out there, you poll, or Comet.

  1. Polling means you ask this question to your server periodically: "is there a new message?" with Ajax. If there is a new message, server returns it or the number of new messages.
  2. Or you can implement some kind of Comet. Comet is a technology which contains open connections between client and server. Since there is an open connection, your server can send new information to clients.

After the number of new messages arrived to you, you can edit your DOM via javascript to show a red notification whereever you want.


You need to use setInterval to periodically check for any new updates and then, if they are found, use ajax to pull in the message.


Easiest way is to use jquery to update the content of a div at a sepecific interval...

#idofdivtoupdate

as it says.. the id of the div you want to auto update...

setinterval is in miliseconds.. (so 5000 checks every 5 seconds)

function updatecontent(){
$('#idofdivtoupdate').load('yourserverscript.php'); 
} 

setInterval("updatecontent()", 5000 );


If ive understood you correctly you need to look up a technology called AJAX, its a combination of JavaScript and XML which allows the sending and receiving of data behind the scenes ie without needed to refresh the page. Theres plenty of tutorials/information out there about it and its not difficult to get the hang of, hope this helps.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜