Javascript to detect if user changes tab

97,254

Solution 1

You can determine if a tab or window is active by attaching a blur / focus event listener to window.

in jQuery it would be

$(window).focus(function() {
    //do something
});
$(window).blur(function() {
    //do something
});

quoted from this SO answer: https://stackoverflow.com/a/1760268/680578

Solution 2

In 2022 you can use an event listener with the visibilitychange event:

document.addEventListener("visibilitychange", (event) => {
  if (document.visibilityState == "visible") {
    console.log("tab is active")
  } else {
    console.log("tab is inactive")
  }
});

Solution 3

If you are targeting browsers that support it, you can use the Page Visibility API available in HTML5. It doesn't directly detect tab changes, per-say, but visibility changes. Which would include (but not limited to) tab changes.

See https://developer.mozilla.org/en/DOM/Using_the_Page_Visibility_API

Solution 4

Best native function hands down, no jQuery.

document.hasFocus

Check the pen, check what happens when you go to the link and back to the codepen tab.

https://codepen.io/damianocel/pen/Yxxzdj

Solution 5

With jQuery:

$(window).on('focus', function () {
});
$(window).on('blur', function () {
});

$().focus & $().blur are deprecated.

Share:
97,254
Maxsteel
Author by

Maxsteel

Everything Python. Loves Django. Aspiring computer scientist. Loves SO. #SOreadytohelp

Updated on January 28, 2022

Comments

  • Maxsteel
    Maxsteel about 1 year


    I am writing a webpage for online quiz. The basic requirement I have is that it must fire an event(stopping the quiz) if the user changes tabs or opens a news window even without minimizing their browser, i.e if the person is attempting to see the answer from some other window/tab. How can I do that?

    Note : Try to avoid including a bleeding edge HTML5 feature in your answer because I want the feature to be supported by all major browsers currently.

  • Matt
    Matt almost 11 years
    Would also fire for just losing focus on the window, but the window is still visible.
  • Maxsteel
    Maxsteel almost 11 years
    Will it be able to detect if the user launches new window/browser without minimizing quiz window?
  • Kristian
    Kristian almost 11 years
    If a new popup occurs, there will be a loss in focus to the current window, wouldn't there?
  • scunliffe
    scunliffe almost 8 years
    This is a handy API however it should be noted that at this time (March 6, 2015) there are some limitations: Alt + Tab (e.g. app switching on of any kind) doesn't register on Windows for Firefox/Chrome/IE10. Switching tabs within each browser does work just fine though. On iOS though app switching or device locking does trigger a visibility change.
  • Eric Uldall
    Eric Uldall almost 8 years
    you also get a false positive when clicking on an iframe in that page.
  • Umair Mohammad
    Umair Mohammad almost 5 years
    Seems to work for me in Opera, FireFox, Safari and Chrome. Haven't tested in others.
  • binarydreams
    binarydreams almost 4 years
    This demo checks for visibility every 200ms and is not ideal in terms of battery use. The Visibility API is event based and works down to IE10 and even on mobile, so I would strongly recommend you use that.
  • BluePie
    BluePie over 2 years
  • katmanco
    katmanco over 2 years
    greate and simple
  • shieldgenerator7
    shieldgenerator7 over 1 year
  • Kamil Bęben 11 months
    This has the advantage over 'visibilitychange' event that it is triggerred after browser's window gained / lost focus while 'visibilitychange' works only on tab switching