Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2004
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Play a sound file with onClick()

    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.

  • #2
    New Coder
    Join Date
    Mar 2005
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Include this tag in your html:

    Code:
    <BGSOUND SRC="#" ID="sound1" autostart="true">
    then include this function:

    Code:
    function playAlert()
    {
    	document.getElementById('sound1').src="error.wav";
    }
    This was the easiest way I found to do it...
    "It doesn't matter if I'm optomistic or not, nothing ever works out for me."

  • #3
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    bgsound is IE only try this
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <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(){
    document.getElementById('music1').innerHTML="";
    }
    //-->
    </script>
    </head>
    <body>
    <a href="http://urltosonghere.mp3" onclick="play(this.href);return false">Play Clip</a>
    <br>
    <a href="#" onclick="stopsong();return false">Stop Clip</a>
    <br><br>
    <span id="music1"></span>
    </div>
    </body>
    </html>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •