多个浏览器中的页面标题通知
我在网页上有一个聊天脚本。 现在,当有人添加消息时,我想通知其他打开页面/选项卡但未活动的人,以接收新消息的通知。
例如,我想将页面标题从“聊天”更改为“新消息 - 聊天”。 我想这也是“Facebook Chat”的作用。
但是当我使用时: document.title = "新消息"; 它仅在提交消息的用户的浏览器中发生变化。不适用于其他浏览器。
我怎样才能实现这个目标?
I have a chat script on a webpage.
Now, when someone adds a message I want to notify other people who have the page/tab open but not active, to receive a notification of a new message.
For instance, I would like to change the page title from 'Chat' to 'New message - Chat'.
I think this is what 'Facebook Chat' does too.
But when I use:
document.title = "New Message";
It only changes in the browser of the user that submitted the message. Not on the other browsers.
How can I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以给你的标题一个id:
或者简单地以
任何一种方式它都会起作用
更新:
在重读之后我犯了一个错误。
当您的脚本检查新消息时,如果有新消息,您还应该更改标题,或者在更改标题之前执行其他测试以查看用户是否处于活动状态
You can give your title an id:
or simply
either way it will work
UPDATE:
after rereading i made a mistake i see.
when your script check for a new message, if there is a new message you should change the title also, or perform additional test to see if there the user is active or not before changing the title
为了实现 Facebook Chat 的功能(即接近实时地向每个用户通知更新),您需要使用“推送”技术或模拟该技术的技术。可能的技术包括轮询(只需让每个客户端定期访问服务器来检查更新)、Comet(长轮询或到服务器的流式连接)或更新且更真实的“推送”(但支持较少)方法,例如服务器发送事件或 WebSocket API。
轮询不是最有效的技术,但实施起来最简单。您可以首先编写一个接受时间戳的服务器端处理程序,并返回自该时间戳以来的更新计数。客户端 Javascript 按一定时间间隔调用该处理程序,传递上次检查的时间戳,并处理响应(如果发现最近的更新,则更新标题栏)。
To do what Facebook Chat does, which is notify each user of updates in close to real time, you need to use "push" technology or something that emulates it. Possible techniques are polling (just having each client regularly hit the server to check for updates), Comet (long polling or streaming connections to the server), or newer and more authentically "push" (but less supported) methods like server-sent events or the WebSocket API.
Polling is not the most efficient technique but will be the simplest to implement. You could start by writing a server-side handler that accepts a timestamp, and returns a count of updates since that timestamp. Client Javascript calls that handler at an interval, passing the timestamp of the last check, and handling the response (updating the title bar if there were recent updates found).
Listen
document.title = "New Message";
是一个绝对完美的代码,但是您将其放在这样一个区域中:当一个人键入消息时,该消息会在他的页面上执行,这是错误的。当用户等待回复(与您聊天的其他用户)时,当 AJAX(或您用来检查的任何其他方法)收到来自键入消息的人的响应时,您应该添加这段代码。基本上我想说的是这段代码被放置在错误的位置。您必须在某人收到聊天消息时放置此代码段,而不是在将其发送给另一个人时放置此代码段。
然后在正文上,您可以在
onmousemove
将标题更改回原来的标题时发出事件!这就是 Facebook 聊天的工作原理。你的错误在于这个地方,你已经放置了你的代码片段,没有别的!
希望这有帮助,干杯!
Listen
document.title = "New Message";
is an absolutely perfect code but you've put this in such an area that when a person types a message, it is executed on his page which is wrong. You should add this piece of code when the user waiting for a reply (the other user, you're chatting with) when the AJAX (or any other method you use to check) receives a response from the person who has typed the message.Basically what I'm saying is that this code piece has been placed in the wrong place. You must place this code piece when a person received the chat message and not when it is dispatched to another person.
And then later on body, you can make event when
onmousemove
change the title back to the original one! So this is how Facebook Chat works.Your mistake is the place, you've place your code piece, nothing else!
Hope this helps, Cheers!