View Full Version : Play a sound file with onClick()

06-30-2005, 04:11 PM
Okay I have been looking all over the net for a script to play a sound with the image/button is clicked... I can't get any to work for some reason. Does anyone have a working script I could use. It would be highly appreciated.

07-01-2005, 01:12 AM
Include this tag in your html:

<BGSOUND SRC="#" ID="sound1" autostart="true">

then include this function:

function playAlert()

This was the easiest way I found to do it...

07-01-2005, 01:22 AM
bgsound is IE only try this

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript"><!--
function play(what){
document.getElementById('music1').innerHTML="<embed type='application/x-mplayer2' id='music2' pluginspage='http://www.microsoft.com/Windows/MediaPlayer/' src='"+what+"' name='MediaPlayer1' width='1' height='1' controltype='2' showcontrols='1' showstatusbar='1' AutoStart='true' loop='false'></embed>";
function stopsong(){
<a href="http://urltosonghere.mp3" onclick="play(this.href);return false">Play Clip</a>
<a href="#" onclick="stopsong();return false">Stop Clip</a>
<span id="music1"></span>