Both checking if the window is focused and focusing on a window/tab is tricky.
That’s partially because the focus/blur
event do not bubble, and partially because the browser window is a part of OS and JavaScript is not integrated with the OS window manager.
Also, the security is important here, because the JavaScript focus/blur
may not override a user’s will.
Still, let’s find out what we can do and see the possible pitfalls.