Cloud9c
Cloud9c

Reputation: 329

mute all audio when users tab away from the site

Is it possible to mute embedded ( tag) audio when the user leaves the site (goes on another tab). I've seen this with other websites where they change their title when the users leave. I'd expect you'd need a bit of javascript and jquery to do this.

Upvotes: 1

Views: 1025

Answers (1)

junvar
junvar

Reputation: 11574

You can listen to the blur event on the window object to detect when the user switches tabs or apps:

window.addEventListener('blur', () => console.log('blur'))

Take care though, as this fires on all occasions when the window loses focus, including for example, pressing ctrl+f to open the find dialog or clicking on an extension popup.

As to audio, assuming you are using standard html5 <audio> element, you can set the muted property to true:

document.getElementById('my-audio-tag').muted = true

Upvotes: 4

Related Questions