Determine if the webpage has a focus / active tab on the background screen?

I would like to turn off the activity of my application if I find that the page is no longer focused. For example, if the page is on the background tab or in another application has focus, I would like to disable the constant polling script or switch modal notifications to the new HTML5 notification API.

Is there any way to get this with JS, and if so, which browsers are supported?

PS - I saw this one , but I don't know if this will work for what I'm looking for. Does anyone have an understanding?

+5
source share
1 answer

blur window, , , focus:

jQuery:

$(window).blur(disableStuff).focus(enableStuff);

JavaScript:

window.onblur = disableStuff;
window.onfocus = enableStuff;
+15

All Articles