Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Play sound when a link is clicked

Posted on 2007-11-26
7
Medium Priority
?
1,052 Views
Last Modified: 2013-12-07
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
0
Comment
Question by:dresdena1
7 Comments
 
LVL 15

Expert Comment

by:spprivate
ID: 20351795
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
0
 

Author Comment

by:dresdena1
ID: 20351829
spprivate,
Thanks for the quick response.
I will already be using the link to go to another page.

dresdena1
0
 
LVL 12

Expert Comment

by:Sinoj Sebastian
ID: 20351879
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:dresdena1
ID: 20352064
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
0
 
LVL 12

Accepted Solution

by:
Sinoj Sebastian earned 500 total points
ID: 20352344
Ok here you go........
------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
      <script type="text/javascript">
            function DHTMLSound(surl) {
              document.getElementById("dummyspan").innerHTML= "<embed src='"+surl+"' hidden=true autostart=true  loop=false>";
            }
            function delayedRedirect(){
                  document.getElementById("dummyspan").innerHTML = ""
                      window.location = "http://www.google.com"
            }
      </script>
</head>
<body >
      <span id=dummyspan></span>
      <A HREF = "#" onclick="DHTMLSound('http://www.phon.ucl.ac.uk/home/mark/audio/success.wav');  setTimeout('delayedRedirect()', 1000); return false;">Play Sound and Redirect</A>


</body>
</html>
------------------------------------------------------------------------
You may change the time out interval, depending up on the length of the sound track.
0
 
LVL 29

Expert Comment

by:Pravin Asar
ID: 20353348
0
 

Author Closing Comment

by:dresdena1
ID: 31411031
sinoj,
Perfect! Thanks very much!

dresdena1
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question