javascript - how to mute Audio/video in website onload for any tag? -
i want mute audio , video sound in website. not particular id or class name. don't know id name can be. track via video/object/div/src html tag name , mute sound.
is possible javascript? have tried following code; not work. appreciate best suggestion.
<!doctype html> <html> <body> <button onclick="enablemute()" type="button">mute sound</button> <video width="320" height="176" controls> <source src="mov_bbb.mp4" type="video/mp4"> <source src="mov_bbb.ogg" type="video/ogg"> browser not support html5 video. </video> <script> var vid = document.getelementsbytagname("video"); function enablemute() { vid.muted = true; } </script> </body> </html>
document.getelementsbytagname
returns collection of objects. if want mute them loop through them , mute them:
document.onload = function() { var videos = document.getelementsbytagname('video'); (var = 0; < videos.length; i++) { videos[i].muted = true; } var audios = document.getelementsbytagname('audio'); (var = 0; < audios.length; i++) { audios[i].muted = true; } }
since tagged question jquery, here jquery variant:
$(document).ready(function() { $('video, audio').prop('muted', true); });
update - how mute iframes
to mute iframes, it's bit different. in same idea, loop through them not have mute
property.
in javascript:
var iframes = document.getelementsbytagname('iframe'); (var = 0; < iframes.length; i++) { iframes[i].contentwindow.postmessage('{"method":"setvolume", "value":0}', '*'); }
jquery variant:
$('iframe').each(function() { this.contentwindow.postmessage('{"method":"setvolume", "value":0}', '*'); });
Comments
Post a Comment