How to know if the tab is active?

0

How can I detect in the browser if the user has the tab (or tab) active ?. Is there any way? Let's say you want to log into the console every time you leave the tab only to know.

    
asked by Theia 16.10.2017 в 18:52
source

2 answers

1

Help me with the link provided by Montoro and here link , which is more useful I have been to add an event listener to "visibilitychange". In this way it would be like this:

document.addEventListener("visibilitychange", function() {
  console.log( document.visibilityState );
});
    
answered by 16.10.2017 в 19:15
0

For example if you have a page that plays music when you have the window active and when not, to create a function that plays the music depending on the value of the parameter, this would be an example with the events that trigger in both actions

<body onFocus="Musica_suena(true)" onBlur="Musica_suena(false)">
    
answered by 16.10.2017 в 19:06