Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 885
mtmurdock Avatar asked Feb 20 '12 17:02

mtmurdock


People also ask

How do you know when a tab has changed?

Detect tab changes with JavaScript To detect the tab change we use pure JavaScript without jQuery etc. Everything you need is hidden in this code. We register the blur event on the global document variable. Generally, the blur and focus events are often used in conjunction.

How do I know if my tablet is focused?

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 I know which tab is clicked in jQuery?

var selectedTab = $("#TabList"). tabs(). data("selected. tabs");

Is focus a jQuery event method?

jQuery focus() MethodThe focus event occurs when an element gets focus (when selected by a mouse click or by "tab-navigating" to it). The focus() method triggers the focus event, or attaches a function to run when a focus event occurs. Tip: This method is often used together with the blur() method.


2 Answers

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 });
like image 139
mtmurdock Avatar answered Sep 21 '22 05:09

mtmurdock


Try using jquery $(selector).focusin or :focus, they both work quite the same way.

http://api.jquery.com/focusin/

http://api.jquery.com/focus-selector/

like image 35
abhig10 Avatar answered Sep 23 '22 05:09

abhig10