jQuery: How can I tell when a tab/window gains focus

mtmurdock picture mtmurdock · Feb 20, 2012 · Viewed 9.2k times · Source

Is there an event that I can register for that fires each time the user clicks on a tab, meaning that they were on my page's tab, clicked on another tab, then came back to my tab.

EDIT: By tabs, I mean browser tabs, not jQueryUI tabs.

Answer

mtmurdock picture mtmurdock · Apr 20, 2012

Matijis provided this answer in a comment, but I wanted to accept it so here it is as an answer.

$(window).on('focus', function() { // your code });