Jump to content

Play sound on click.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
iConquest

iConquest

    Newbie

  • Members
  • PipPip
  • 18 posts
Hey :)

On my website, I've got a div at the bottom which is basically just an image that changes when you put your mouse over it:

Before mouseover:

Posted Image

On mouseover:

Posted Image

I want a sound to play when you click it.

I found a way to do this using javascript.

Here's the script:

<script language="javascript" type="text/javascript">

function playSound(soundfile) {

document.getElementById("dummy").innerHTML=

"<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";

}

</script>

and here's the code for the images (including the mouseclick code to play the sound):

<span id="dummy" onclick="playSound('images/where.mp3');"><a onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Bottom-1','','images/dateover.png',1)"><img src="images/date.png" name="Bottom-1" width="115" height="45" border="0" id="Bottom-1"  /></a></span>

When I click the image, the sound plays but it also gets rid of the images:

Posted Image

This happens to everything that I try and use the sound on. For example, if I tried to click one word in <SPAN> tags, the sound would play but the word would disappear.

Is there a way to stop stuff disappearing when I want the sound to play?

Thanks.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It looks like you're REPLACING the contents of the div tag with the sound tag. Try this (untested):
<script language="javascript" type="text/javascript">
function playSound(soundfile) {
document.getElementById("dummy").innerHTML=document.getElementById("dummy").innerHTML +
"<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
}
</script>

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
iConquest

iConquest

    Newbie

  • Members
  • PipPip
  • 18 posts
It worked! Thanks a lot! :)

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Whew! I'm glad it worked :)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
j_berkobien

j_berkobien

    Newbie

  • Members
  • Pip
  • 1 posts
I've been working to try and achieve a similar goal of both an image rollover and an Onclick MP3 Sound. I used the codes you gave, but the problem is:

When I click the image once, it works perfectly.
When I click the image the second time, it plays the sound two times.
When I click the image the third time, it plays the sound three times.
...etc...

I'm looking to have an image rollover that only plays once each time it is clicked. Does that make any sense? What can I do?