View Full Version : Play sound onMouseOver

12-20-2010, 09:21 AM
Just so you know: this is not my idea, the person whose site this is wants a short blip sound played whenever you roll over the links in the sidebar. I know it's not advised, but she insists.

So can anybody tell me how this is done? I've tried implementing a few scripts but no luck in Firefox or Chrome.


Philip M
12-20-2010, 10:08 AM
As you say, this is very ill-advised, but if your client really insists on driving people away from her website:-

Place the following script in the <head> of your HTML document:

<script type="text/javascript">
function playSound(soundfile) {
document.getElementById("dummy").innerHTML="<embed src=\""+ soundfile +"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
The Sound is Placed in an Empty Span<br>
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:-

<a href="#" onclick="playSound('URL to soundfile');">Click here to hear a sound</a>

<p onmouseover="playSound('URL to soundfile');">Mouse over this text to hear a sound</p>

"I'm all in favour of keeping dangerous weapons out of the hands of fools. Let's start with typewriters." - Frank Lloyd Wright

12-24-2010, 04:26 AM
Thanks a lot. Appreciate it. Hopefully I'll never have to use it again.