javascript - Possible to make browser play a sound file when a certain condition is met? -


this question has answer here:

for javascript:

is possible browser play sort of sound/music when image appears? perhaps play mp3 file in music library? right now, best can is:

if (document.getelementbyid("happyface")) {     window.alert("attention: happy face detected.");     } 

i should mention have page refreshing itself, loop isn't necessary (at least, don't think so).

just use audio tag that:

function sound(){     var audio = document.createelement("audio");     audio.src = "your_path_to_soundfile.wav";     audio.addeventlistener("ended", function () {         document.removechild(this);     }, false);     audio.play();    }  if (document.getelementbyid("happyface")) {     sound(); } 

Comments