Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to detect if a browser window is not currently active?

I have JavaScript that is doing activity periodically. When the user is not looking at the site (i.e., the window or tab does not have focus), it'd be nice to not run.

Is there a way to do this using JavaScript?

My reference point: Gmail Chat plays a sound if the window you're using isn't active.

like image 926
Luke Francl Avatar asked Jun 29 '09 19:06

Luke Francl


People also ask

How do I know if my 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.

How do I check if a Javascript window is active?

You can use the focus and blur events on the window object to check if the window is still active like so: window. addEventListener('focus', function (event) { console. log('has focus'); }); window.

How do I check my browser focus?

Use the visibilitychange event to detect if a browser tab has focus or not, e.g. document. addEventListener('visibilitychange', checkTabFocused) . The event is fired at the document when the contents of its tab have become visible or have been hidden.

How do you check whether the browser tab is active or not in jquery?

$(window). hover(function(event) { if (event. fromElement) { console. log("inactive"); } else { console.


2 Answers

Since originally writing this answer, a new specification has reached recommendation status thanks to the W3C. The Page Visibility API (on MDN) now allows us to more accurately detect when a page is hidden to the user.

document.addEventListener("visibilitychange", onchange); 

Current browser support:

  • Chrome 13+
  • Internet Explorer 10+
  • Firefox 10+
  • Opera 12.10+ [read notes]

The following code falls back to the less reliable blur/focus method in incompatible browsers:

(function() {   var hidden = "hidden";    // Standards:   if (hidden in document)     document.addEventListener("visibilitychange", onchange);   else if ((hidden = "mozHidden") in document)     document.addEventListener("mozvisibilitychange", onchange);   else if ((hidden = "webkitHidden") in document)     document.addEventListener("webkitvisibilitychange", onchange);   else if ((hidden = "msHidden") in document)     document.addEventListener("msvisibilitychange", onchange);   // IE 9 and lower:   else if ("onfocusin" in document)     document.onfocusin = document.onfocusout = onchange;   // All others:   else     window.onpageshow = window.onpagehide     = window.onfocus = window.onblur = onchange;    function onchange (evt) {     var v = "visible", h = "hidden",         evtMap = {           focus:v, focusin:v, pageshow:v, blur:h, focusout:h, pagehide:h         };      evt = evt || window.event;     if (evt.type in evtMap)       document.body.className = evtMap[evt.type];     else       document.body.className = this[hidden] ? "hidden" : "visible";   }    // set the initial state (but only if browser supports the Page Visibility API)   if( document[hidden] !== undefined )     onchange({type: document[hidden] ? "blur" : "focus"}); })(); 

onfocusin and onfocusout are required for IE 9 and lower, while all others make use of onfocus and onblur, except for iOS, which uses onpageshow and onpagehide.

like image 149
19 revs, 10 users 67% Avatar answered Sep 17 '22 08:09

19 revs, 10 users 67%


I would use jQuery because then all you have to do is this:

$(window).blur(function(){   //your code here }); $(window).focus(function(){   //your code }); 

Or at least it worked for me.

like image 44
Carson Wright Avatar answered Sep 19 '22 08:09

Carson Wright