How can I mute all sound on my page with JS?
This should mute HTML5 <audio>
and <video>
tags along with Flash and friends.
This can easily be done in vanilla JS:
// Mute a singular HTML5 element
function muteMe(elem) {
elem.muted = true;
elem.pause();
}
// Try to mute all video and audio elements on the page
function mutePage() {
var elems = document.querySelectorAll("video, audio");
[].forEach.call(elems, function(elem) { muteMe(elem); });
}
or in ES6:
// Mute a singular HTML5 element
function muteMe(elem) {
elem.muted = true;
elem.pause();
}
// Try to mute all video and audio elements on the page
function mutePage() {
document.querySelectorAll("video, audio").forEach( elem => muteMe(elem) );
}
This, of course, only works with <video>
or <audio>
elements, as items like Flash or JS initialized audio is impossible to restrict in general.