Different Ways to Play a Sound from a Web Page

Back to menu

3. Controlling an Embedded Sound using JavaScript (Old)

A slight improvement on method 2 can be gained by making the embedded player invisible and controlling it from JavaScript. There are two parts to this: firstly a function (here called EvalSound) which takes the "name" of the hidden embedded player and calls its Play() method. Secondly a link, image or button which calls the function on demand when the component is clicked or has the mouse rolled onto it.

Here is the first part: the embedded sound and the EvalSound() function:

    <script>
    function EvalSound(soundobj) {
      var thissound= eval("document."+soundobj);
      thissound.Play();
    }
    </script>
    
    <embed src="success.wav" autostart=false width=0 height=0 name="sound1"
    enablejavascript="true">
    

Here are examples of a link, an image and a button calling the function.

    <a href="#" onMouseOver="EvalSound('sound1')">Move mouse here</A>
    

    <img src="play.gif" onClick="EvalSound('sound1')">
    

    <form>
    <input type="button" value="Play Sound" onClick="EvalSound('sound1')">
    </form>
    


If this doesn't work, look here for more help on plug-ins for audio replay.

Compatibility: Internet Explorer, Firefox, Opera, Chrome.

Next method ...