Link to home
Start Free TrialLog in
Avatar of dresdena1
dresdena1

asked on

Play sound when a link is clicked

EE,
I am trying to find code for JavaScript that will play a sound (.wav file) when a link is clicked. I do not want the sound to play when the page loads and I am only going to associate it with a single link on a page.

I would like it to play in IE and FireFox, but if I can get it work only in IE that would fine.

Any help would be greatly appreciated.

dresdena1
Avatar of spprivate
spprivate
Flag of United States of America image

Just create a link to this wav file like any other hyperlink.Rest will be taken care by the browser.
<A HREF = "mymusic.wav" >Listen Sound </A>
Thats it
Avatar of dresdena1
dresdena1

ASKER

spprivate,
Thanks for the quick response.
I will already be using the link to go to another page.

dresdena1
Try this
------------------------------------
function DHTMLSound(surl) {
  document.getElementById("dummyspan").innerHTML=
    "<embed src='"+surl+"' hidden=true autostart=true loop=false>";
}

<span id='dummyspan'></span>
<form>
<img src="play.gif" onClick="DHTMLSound('success.wav')">
</form>
---------------------------------------
This should work in IE and FF
sinoj,
I am trying to get this to work on a horizontal line of text links. I believe that the form tag is causing a line break.
To try to get sound to play, I enclosed the <a href in the form tags and replaced the img src with a href and then put the DHTMLSound with the sound file that I am interested in playing.
It didn't work.

Do I need to enclose the function in <script>  tags?
It showed through the browser so I enclosed in <script> tags to hide it and I also tried putting it in the <HEAD> section.

I am still unable to get any sound.

Here is my last try:
<<
<script>
function DHTMLSound(surl') {
  document.getElementById("dummyspan").innerHTML=
    "<embed src='"+surl'+"' hidden=true autostart=true loop=false>";
}
</script>

<span id='dummyspan'></span>
<form>
<a href="race_schedule.html" onClick="DHTMLSound('cowbell.wav')">Races</a>&nbsp;&nbsp;&nbsp;
</form>
>>

Any ideas?
Thanks very much
 dresdena1
ASKER CERTIFIED SOLUTION
Avatar of Sinoj Sebastian
Sinoj Sebastian
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Pravin Asar
sinoj,
Perfect! Thanks very much!

dresdena1