Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect whether a browser window is focused or not?

Tags:

javascript

My page should make notification sounds only when the window is in the background. I can track the window.onfocus and window.onblur events to notice when the focus state changes. However, I don't know whether the window will load focused or not, since it might load in a background tab, for instance.

How do I decide whether to play sounds or not before I get an onfocus/onblur event?

like image 832
Peeja Avatar asked May 06 '09 20:05

Peeja


People also ask

How do I know if my browser window has focus?

Use the document. hasFocus() method to check if a window has focus, e.g. if (document. hasFocus()) {} . The method returns a boolean value indicating whether the document or any element in the document has focus.

What is window focus?

focus() Makes a request to bring the window to the front. It may fail due to user settings and the window isn't guaranteed to be frontmost before this method returns.

How can I tell if Windows is active?

Start by opening the Settings app and then, go to Update & Security. On the left side of the window, click or tap Activation. Then, look on the right side, and you should see the activation status of your Windows 10 computer or device.


1 Answers

Couldn't you just assume it's loaded blurred, and then change the status to focused as soon as you receive any type of event (keydown/mousemove)?

like image 159
Jed Schmidt Avatar answered Sep 18 '22 13:09

Jed Schmidt