A number of other answers have covered how a running Javascript script can tell whether its current window is active or not. However, that's a pretty restrictive set of choices, and makes no distinctions between (for example)
All of these count as inactive, despite the fact that in the latter two cases the webpage remains visible to the user.
While there's evidence that this may be technically impossible given how web browser sandboxes work, I'd still like to be able to detect the differences between the above. For example, pausing an animation for (1) or (2), but keeping it going for (3) or (4). I'm wondering if Javascript (plus frameworks, I guess, or WebAssembly) provides a mechanism to determine this - or, failing that, whether any particular browsers have mechanisms to determine it from within the javascript code of one page.
You can't. I looked into this for a project a while back and active vs inactive is the only distinction the browser can make.
Information about the positions of other windows on the system isn't passed to the browser, especially given that this route could possibly be reverse engineered to expose a security risk. It's much safer to keep each window in the dark about what other programmes on the system are doing.
You can gather information based on events happening in the current window - for example detecting the opening of a print dialogue - but not from other windows.
The Page Visibility API has 95% coverage and is probably the closest you can come - but you already know this, because it's the top answer in each question you linked. You're out of luck here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With