Hello!

How can I track if a user is on a tab? Those. the page loads, I click on another browser tab and a certain function is executed on my page. When I click the page tab again, another function is executed.

Thank you in advance.

    1 answer 1

    This can be done with the usual focus and blur events by applying them to the window object.

    Example:

    $(window).focus(function() { console.log( 'Вкладка открыта! :)' ); }); $(window).blur(function() { console.log( 'Вкладка не открыта! :(' ); }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

    Example without jQuery:

     window.onfocus = function() { console.log( 'Вкладка открыта! :)' ); }; window.onblur = function() { console.log( 'Вкладка не открыта! :(' ); };