简体   繁体   English

如何判断IE8窗口何时失去焦点?

[英]How to tell when the IE8 window loses focus?

I have been using this, although I did not realize it was functioning improperly in ie8 until recently. 我一直在使用它,尽管直到最近我才意识到它在ie8中无法正常运行。

$(window).blur(function () {
 alert("lost");
});

In firefox or chrome or safari, this properly results in an alert being shown when the window loses focus. 在Firefox,Chrome或Safari中,当窗口失去焦点时,这会适当地导致显示警报。 However, in IE8 it seems that the alert is put into some sort of queue. 但是,在IE8中,警报似乎已放入某种队列中。 The alert "lost" only shows when the window regains focus. 警报“丢失”仅在窗口重新获得焦点时显示。 What is more confusing is that when coupled with an event which tracks if the window gains focus, they go out of order. 更令人困惑的是,当与跟踪窗口是否获得焦点的事件结合使用时,它们会混乱。

$(window).focus(function () {
 alert("gained");
});

(don't try this in chrome or firefox because the alerts will enter some sort of cycle) (请勿在chrome或Firefox中尝试此操作,因为警报会进入某种循环)

If both of those are used with IE8, when the window loses focus, and then regains it, IE8 alerts "gained" ok "lost". 如果这两个都与IE8,当窗口失去焦点,并重新获得其使用,IE8警报“获得了” ok “丢失”。 This out of order event firing causes my code issues because it is backwards, and reports that the last event was the browser losing focus. 这种乱序事件触发会导致我的代码问题,因为它是向后的,并报告最后一个事件是浏览器失去焦点。

How can I track this in IE8? 如何在IE8中跟踪此内容?

credit for approach: https://stackoverflow.com/a/10999831/1026459 方法的功劳: https : //stackoverflow.com/a/10999831/1026459

Solution for my situation: 我的情况的解决方案:

document.onfocusout = function(){
 alert("lost");
};

This actually controls an interval, but that is besides the point. 这实际上控制了一个时间间隔,但这不是重点。 onfocusout works for ie8, chrome, safari, and ff . onfocusout适用于ie8, chrome,safari和ff I am not sure what issue ie8 had with blur but I am phasing it out. 我不确定ie8出现什么blur问题,但我正在逐步淘汰它。

EDIT 编辑

Unfortunately document.onfocusout did not work in chrome or safari so I had to do more workaround. 不幸的是document.onfocusout在chrome或safari中不起作用,所以我不得不做更多的解决方法。 Here is what I ended up with. 这就是我最后得到的。

    //ie workaround
    document.onfocusout = function(e){
        if( e === undefined ){//ie
            var evt = event;//ie uses event
            if( evt.toElement == null ){//check where focus was lost to
             console.log("lost");
            }
        }
    };

    window.onblur = function(e){
     if( e !== undefined ){//ie will have an undefined e here, so this screens them out
      console.log("lost");
     }
    };

    $(window).focus(function () {
     console.log("gained");
    });

After replicating this in IE8, I tested if the page visibility shim by Mathias Bynens worked and it seems to solve your problem. 在IE8中将其复制后,我测试了Mathias Bynens的页面可见性填充程序是否起作用,并且似乎可以解决您的问题。 You can download it here: http://mths.be/visibility . 您可以在此处下载: http : //mths.be/visibility

I did change your testing code a little bit to avoid getting into an alert-loop; 我确实对测试代码做了一些更改,以避免进入警报循环。

$(document).on({
    'show.visibility': function () {
        $(document.body).append(new Date().getTime() + '<br/>');
    },
    'hide.visibility': function () {
        $(document.body).append(new Date().getTime() + '<br/>');
    }
});

Do also note that this does have slightly different behavior from $(window).blur() and $(window).focus() , rather then activating whenever the user clicks outside the window-element, in most browsers (other then IE) this will only activate when the user can no longer see the window (eg switches tabs, or minimizes the browser, but not when changing to a different application or monitor). 还要注意,这与$(window).blur()$(window).focus()行为稍有不同,在大多数浏览器(IE之外的其他浏览器$(window).focus() ,只要用户单击窗口元素外,它$(window).focus()激活仅当用户无法再看到窗口时(例如,切换选项卡或最小化浏览器,而切换到其他应用程序或监视器时,才激活)。 I personally favor that behavior considering I wouldn't want the website to change while I am still watching it but interacting with a different application. 我个人比较喜欢这种行为,因为我不想在我仍在观看但要与其他应用程序交互时更改网站。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM