dA_uNknOwN dA_uNknOwN - 1 year ago 152
Javascript Question

Javascript Audio Play on click

I have a javascript code to start a sound on click .
It works on chrome but on fireforx it startes onload ,but i want it onclick theire too.
Can anyone help?

var audio = new Audio("http://music.ogg" ) ;

audio.oncanplaythrough = function(){

audio.loop = true;

audio.onended = function(){


<input type="image" src="http://button.png" onclick="audio.play()">

Answer Source

Try this


  function play(){
       var audio = document.getElementById("audio");

<input type="button" value="PLAY"  onclick="play()">
<audio id="audio" src="http://dev.interactive-creation-works.net/1/1.ogg" ></audio>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download