Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can javascript code tell if the browser is in the background?

I would like to tell whether my browser's tab is in focus, and whether the browser itself is in the foreground or in the background.

Is there a javascript interface that queries the foreground/background status of the browser?

Update Following Palpatim's answer below, I found this practical HTML5Rocks tutorial about the page visibility API.

like image 591
Adam Matan Avatar asked Feb 02 '26 18:02

Adam Matan


1 Answers

Your browser support requirements will determine whether this is viable, but the Page Visibility API may get you part of the way there.

It doesn't determine foreground/background status; to my knowledge there is no API available to JavaScript that will give you that level of access, so you'd have to rely on a browser plugin to report it. However, it will report whether a page is visible or not, which for many use cases is all that's required.

like image 184
Palpatim Avatar answered Feb 05 '26 06:02

Palpatim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!