I am trying to play sounds when my mouse rolls over images. I want to implement sound into this code. When the mouse rolls over the image, it not only change and displays a copy of the 2nd image, but it also plays a sound. Whats the best way to implement sound?

1 answer

This script will add sound effects to onclick and onmouseover attributes of elements. Be sure to test them in different browsers, as not all Web browsers handle onmouseover and onclick attributes on elements other than links.
Use JavaScript to Add Sound

The JavaScript places an embed tag inside an empty span tag when the script is initiated. So, you need to add the following span file somewhere within the body of your HTML page, preferably near the top of the document:

<span id="dummy"></span>

Call the Script with an onmouseover or onclick Attribute

The last thing you need to add is an element that you want to generate the sound on click or on mouseover. Call the script with one of those attributes: <p onmouseover="playSound('URL to soundfile');">&nbsp;</p>

Thanks, however, I don't understand the foward slashes. Is the code right? — Battle Tested over 9 years ago

back slashes "\" you mean? It's to 'escape' the subsequent character. In this case, it is to escape the subsequent double quote character so that it doesn't conflict with the opening double quote for the string and close it. This will work too, ` src='"+soundfile+"' hidden='true' autostart='true' loop='false' `. — o.k.w over 9 years ago