jQuery - 窗口焦点、模糊事件未触发 - 适用于 Firefox 和 Chrome
简而言之; 我编写了一个简单的聊天应用程序供我和我的朋友使用。 当运行应用程序的窗口没有焦点(最小化或位于其他窗口后面)并且出现消息时,我想更改窗口标题栏以充当警报。 就像 Google 的聊天应用程序在 GMail 中所做的那样。
在 Firefox 和 Chrome 中一切正常,但在 IE7 中则不然(尚未测试 8)。
这是我用来确定窗口是否具有焦点的代码。 是否可以以不同的方式编写以便在 IE 中也能工作? 另外,我愿意接受任何其他方法来完成同样的事情。 提前谢谢了。
$(window).bind("blur", function() {
hasfocus = false;
});
$(window).bind("focus", function() {
hasfocus = true;
});
In a nutshell; I wrote a simplistic chat application for a buddy and me to use. When the window running the application does not have the focus (minimized or behind other windows) and a message comes in, I want to change the windows title bar to serve as an alert. Exactly like Google's chat application does in GMail.
Everything works flawlessly in Firefox and Chrome but not in IE7 (haven't tested 8).
This is the code I am using to determine if the window has focus. Can this be written differently to also work in IE? Also, I'm open to any other approaches to accomplish the same thing. Many thanks in advance.
$(window).bind("blur", function() {
hasfocus = false;
});
$(window).bind("focus", function() {
hasfocus = true;
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不认为谷歌聊天使用窗口来检查焦点。 它使用与您聊天的用户的文本框。 一旦文本框获得焦点,“说...”就会停止循环。
您可能需要检查鼠标移动以查看窗口是否具有焦点。 除此之外,我仍在尝试弄清楚如何在尝试保持页面活动时检查窗口的焦点。
I don't think google chat uses the window to check focus. It uses the textbox of the user chatting to you. As soon as the textbox receives focus " Says..." stops looping.
You might want to check for mouse movements to see if the window has focus. Other than that, I am still trying to figure out how to check the window for focus when trying to keep a page live.
这段 jquery 可以在 IE 和所有好的浏览器(chrome、ff 等)中工作。 关键是文档 focusin\focusout 用于 IE 支持。
This bit of jquery will work in IE and all the good browsers (chrome, ff etc). The key is document focusin\focusout for IE support.
如果您尝试绑定到文档元素会发生什么?
What happens if you attempt to bind to the document element?