Detect whether a window is visible [duplicate]

给你一囗甜甜゛ 提交于 2019-12-02 04:55:49

问题


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

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.

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.

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


回答1:


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 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;
 }

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

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



来源:https://stackoverflow.com/questions/12536562/detect-whether-a-window-is-visible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!