riv
riv

Reputation: 7324

Page visibility API - check if browser is in background?

According to http://www.w3.org/TR/page-visibility/,

On getting, the hidden attribute MUST return true if the Document contained by the top level browsing context (root window in the browser's viewport) [HTML5] is not visible at all. The attribute MUST return false if the Document contained by the top level browsing context is at least partially visible on at least one screen.

However, using the following script:

setInterval(function() {console.log(document.hidden);}, 1000)

it keeps printing false when the browser is running in the background, i.e. not visible at all. It does print true if I switch to a different tab, or minimize the browser, however.

Upvotes: 3

Views: 1390

Answers (1)

mofojed
mofojed

Reputation: 1393

PageVisibility API returns true only if the API is completely hidden. If your window is in the background, but even partially visible, it will return false.

Try putting it in the background, making it smaller than another window, then placing that window overtop. It now returns true.

You may want something like document.hasFocus instead (or some combination).

Upvotes: 1

Related Questions