is there a way to detect if my website runing in background , for example : my website is open in browser but the user standing on the other tab youtube for example.
-
You want to know if your website's scripts are still running ? Well, I'd tend to say that yes it is. Depending on the browser, but by now multi-tasking is really popular. – Xcrowzz Aug 19 '15 at 09:58
4 Answers
Crossbrowser & lightweight way to check if user is looking at the page or interacting with it.
Use Page Visibility API, demo
The Page Visibility API lets you know when a webpage is visible or in focus. With tabbed browsing, there is a reasonable chance that any given webpage is in the background and thus not visible to the user. When the user minimizes the webpage or moves to another tab, the API sends a visibilitychange event regarding the visibility of the page. You can detect the event and perform some actions or behave differently. For example, if your web app is playing a video, it would pause the moment the user looks at another browser, and plays again when the user returns to the tab. The user does not lose their place in the video and can continue watching.

- 6,259
- 4
- 24
- 40
Yes, window.onfocus
and window.onblur
should work for your scenario.

- 101,612
- 66
- 270
- 352

- 6,976
- 2
- 28
- 48
-
Blur event even fires if we have clicked on Address Bar of browser, is there any way we can track this case? – dqureshiumar Dec 19 '22 at 05:53
use jQuery blur for this:
$(window).blur(function(){ console.log("blur"); })
tested in my console on stackoverflow and it throws the console message when I go to a different tab.

- 647
- 4
- 12
Check This below link. I hope you get your answare
1.Possible to detect if a user has multiple tabs of your site open?

- 1
- 1