jQuery - 窗口焦点、模糊事件未触发 - 适用于 Firefox 和 Chrome

发布于 2024-07-30 04:33:53 字数 437 浏览 4 评论 0原文

简而言之; 我编写了一个简单的聊天应用程序供我和我的朋友使用。 当运行应用程序的窗口没有焦点(最小化或位于其他窗口后面)并且出现消息时,我想更改窗口标题栏以充当警报。 就像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

辞别 2024-08-06 04:33:53

我不认为谷歌聊天使用窗口来检查焦点。 它使用与您聊天的用户的文本框。 一旦文本框获得焦点,“说...”就会停止循环。

您可能需要检查鼠标移动以查看窗口是否具有焦点。 除此之外,我仍在尝试弄清楚如何在尝试保持页面活动时检查窗口的焦点。

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.

美胚控场 2024-08-06 04:33:53

这段 jquery 可以在 IE 和所有好的浏览器(chrome、ff 等)中工作。 关键是文档 focusin\focusout 用于 IE 支持。

$(function(){
    $(window).bind('blur', function(){
        console.debug('window blur');
    });

    $(window).bind('focus', function(){
        console.debug('window focus');
    });
    // IE EVENTS
    $(document).bind('focusout', function(){
        alert('document focusout');
    });

    $(document).bind('focusin', function(){
        alert('document focusin');
    });
});

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.

$(function(){
    $(window).bind('blur', function(){
        console.debug('window blur');
    });

    $(window).bind('focus', function(){
        console.debug('window focus');
    });
    // IE EVENTS
    $(document).bind('focusout', function(){
        alert('document focusout');
    });

    $(document).bind('focusin', function(){
        alert('document focusin');
    });
});
蔚蓝源自深海 2024-08-06 04:33:53

如果您尝试绑定到文档元素会发生什么?

What happens if you attempt to bind to the document element?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文