简体   繁体   English

如何检测浏览器选项卡是否集中在顶部?

[英]How to detect if browser tab is focused and on top?

I have a page that opens a word document for the user. 我有一个页面可以为用户打开Word文档。 Behind, on the page, a 'wait dialog' is rendered to be closed when user closes the opened document. 在页面的后面,当用户关闭打开的文档时,将显示一个“等待对话框”以将其关闭。 I did some digging and I did not find solution that would work. 我做了一些挖掘,但没有找到可行的解决方案。

Of course I checked this one: Is there a way to detect if a browser window is not currently active? 我当然检查过这一点: 是否可以检测浏览器窗口当前是否未激活? , but it only works when I'm switching tabs, or minimizing browser window. ,但仅在我切换标签或最小化浏览器窗口时有效。 It doesn't detect that the browser opened a window with file, neither the browser getting the focus back because the window with file was closed. 它没有检测到浏览器打开了一个带有文件的窗口,也没有检测到浏览器没有重新获得焦点,因为没有关闭带有文件的窗口。

I tested above solution (and others I'm aware of, mostly focusing on hooking up to focus event) on IE11. 我在IE11上测试了上述解决方案(以及我所知道的其他解决方案,主要集中在挂钩焦点事件上)。 One complication might be that the new window is opened from the iframe which is in the iframe (don't ask, I can't change that :( ), but I was using window.top, and also checking window.top.document.hidden value, which was all the time false. 一种复杂的情况可能是从iframe中的iframe打开了新窗口(不要问,我无法更改:(),但是我使用的是window.top,还检查window.top.document .hidden值,一直都是假的。

EDIT: How to tell if browser/tab is active - this question doesn't solve the problem as well. 编辑: 如何判断浏览器/选项卡是否处于活动状态 -这个问题也不能解决问题。 As I mentioned before, it might be because of these iframes (I'm using window.top to attach events, but still no effect): 如前所述,这可能是由于这些iframe(我正在使用window.top附加事件,但仍然没有效果):

Structure: 结构体:

  • Page
    • Iframe triggering action create file iframe触发操作创建文件
    • iframe implementing creating file iframe实现创建文件
    • iframe adding file to store, and opening the new window iframe添加要存储的文件,然后打开新窗口

Java script used to implement this actions is included in all iframes and in the page as well. 用于执行此操作的Java脚本也包含在所有iframe和页面中。 I know this might be vague explanation, and the recommended solution would be to change architecture of this web app... 我知道这可能是含糊的解释,建议的解决方案是更改此Web应用程序的体系结构。

          var vis = (function(){
            var stateKey, 
                eventKey, 
                keys = {
                        hidden: "visibilitychange",
                        webkitHidden: "webkitvisibilitychange",
                        mozHidden: "mozvisibilitychange",
                        msHidden: "msvisibilitychange"
            };
            for (stateKey in keys) {
                if (stateKey in document) {
                    eventKey = keys[stateKey];
                    break;
                }
            }
            return function(c) {
                if (c) document.addEventListener(eventKey, c);
                return !document[stateKey];
            }
        })();


        vis(function(){

            if(vis()){

                // tween resume() code goes here    
            setTimeout(function(){            
                    console.log("tab is visible - has focus");
                },300);     

            } else {

                // tween pause() code goes here
                console.log("tab is invisible - has blur");      
            }

        });

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

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