检测窗口是否可见 [英] Detect whether a window is visible

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

问题描述

可能重复:
JavaScript:是否存在有什么方法可以检测到该窗口当前处于活动状态? (即正在显示在活动标签/窗口上)

Possible Duplicate:
Javascript: Is there any way to detect that window is currently active? (i.e. is beening shown on active tab/window)

我有一个基于Web的仪表板,可在医院地板上向护士等显示对时间敏感的重要消息.面临的挑战是,如果仪表板未打开或未最小化,则他们将看不到该消息并能够做出响应.我目前正在跟踪应用程序运行的时间百分比(它每10秒钟通过AJAX轮询一次新消息,以便我每小时可以轻松地查看有多少个请求,等等),但这并不能告诉我该应用程序是否已最小化

I have a web-based dashboard which displays critical time-sensitive messages to nurses etc. on a hospital floor. The challenge is that if the dashboard is not open or is minimized, they will not see the message and be able to respond. I currently track percent of time that the app is running (it polls for new messages over AJAX every 10 seconds so I can easily see how many requests it does per hour or whatever), but that doesn't tell me whether the app was minimized.

一种方法是使用onfocus/onblur,但效果有限,因为部署此方法的大多数计算机都是双显示器计算机,使用它们时,通常会使仪表板在一个屏幕上打开.在另一个屏幕上显示临床信息系统.使用focus/blur方法,该状态将被计为未聚焦",但我希望其统计信息被计为开放".

One method would be to use onfocus/onblur, but that has limited effectiveness since most of the computers where this is deployed are dual-monitor machines and they often will keep the dashboard open on one screen while using the Clinical Information System on the other screen. This state will count as "unfocused" with the focus/blur method, but I want it to count as "open" for my stats.

关于检测窗口可见性的任何想法?我在想可能包含一个微小的Flash像素"并检测是否可见-还有其他想法吗?

Any ideas for detecting window visibility? I'm thinking maybe include a tiny Flash "pixel" and detect whether that is visible - any other ideas?

推荐答案

您可以使用页面可见性API 在支持它的浏览器中.否则,正如您所说的,您将需要使用onfocus/onblur进行破解.

You can use the Page Visibility API in browsers that support it. Otherwise as you said you will need to use a hack with onfocus/onblur.

visibility.js 是一个可在所有浏览器中抽象化的库.如果浏览器不支持Page Visibility API,它将使用onblur/onfocus hack.

visibility.js is a library that abstracts this across all browsers. It will use the onblur/onfocus hack if the browser does not support the Page Visibility API.

function isPageHidden(){
     return document.hidden || document.msHidden || document.webkitHidden || document.mozHidden;
 }

来源: http://www.nczonline.net/blog/2011/08/09/introduction-to-the-page-visibility-api/

阅读: https://developer.mozilla.org/en-US/docs/DOM/Using_the_Page_Visibility_API

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

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