主动窗口检测 [英] Active window detection

查看:86
本文介绍了主动窗口检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在检查当前选项卡/窗口是否处于焦点状态或不执行某些特定任务.当我切换选项卡但打开选项卡并打开软件(音乐)时,以下功能可以正常工作以检测焦点播放器/窗口文件夹)保持选项卡处于活动状态/打开状态,该功能仍将窗口/选项卡视为焦点!我试图实现的情况是检测到由于打开应用程序/软件而导致窗口/选项卡失去焦点当前关注的窗口/标签.如果您根据我的代码提供一个jsfiddle答案,那将是非常不错的选择!

I am checking that the current tab/window is focused or not to perform some particular task.The following function is correctly working to detect the focus when i am switching tabs but when the tab is open and i open a software(music player/windows folders) keeping the tab active/open, the function still consider the window/tab as focused!The situation that i am trying to achieve is to detect that the window/tab is lost the focus due to opening application/software upon currently focused window/tab.It will be great if you provide a jsfiddle with your answer based on my code!

$(document).ready(function() {

        var hidden, change, vis = {
            hidden: "visibilitychange",
            mozHidden: "mozvisibilitychange",
            webkitHidden: "webkitvisibilitychange",
            msHidden: "msvisibilitychange",
            oHidden: "ovisibilitychange" /* not currently supported */
        };             
    for (hidden in vis) {
        if (vis.hasOwnProperty(hidden) && hidden in document) {
            change = vis[hidden];
            break;
        }
    }
    if (change)
        document.addEventListener(change, onchange);
    else if (/*@cc_on!@*/false) // IE 9 and lower
        document.onfocusin = document.onfocusout = onchange
    else
        window.onfocus = window.onblur = onchange;

    function onchange (evt) {
        evt = evt || window.event;
        if (evt.type == "focus" || evt.type == "focusin")
           window_focus = true;
        else if (evt.type == "blur" || evt.type == "focusout")
           window_focus = false;
        else        
           window_focus = this[hidden] ? false : true;
    }

});

框架:

<frameset rows="130,*" style="border: 1px #CC3333"  noresize="noresize" ">
<frame name="showcountframe" src="https://jsfiddle.net/nvobhaor/1/" scrolling=no marginheight="2" marginwidth="2"  noresize="noresize">
<frame name="showcontframe" src="showcontframe.html" marginheight="0" marginwidth="0" noresize="noresize">
</frameset><noframes></noframes>

推荐答案

您可以为此使用window.onfocuswindow.onblur而不是那段代码墙.所有主要浏览器都支持它,并且可以很好地满足您的目的.例如,它可以检测您何时更改选项卡,或按您所说的打开另一个应用程序.

You could use window.onfocus and window.onblur for this instead of that wall of code. It is supported by all the main browsers and works perfectly fine for the purpose you want. It detects when you change tabs, for example, or open another application as you said.

示例:

window.onfocus = function() {
    console.log('Focused');
};

window.onblur = function() {
    console.log('Not focused');
};

这篇关于主动窗口检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆