PDA

View Full Version : onClick sound in javascript



suzzy
12-09-2011, 08:37 PM
Hi there!

I'm trying to use a sound file onClick using this helpful script:

http://www.dynamicdrive.com/dynamicindex11/javascriptsound.htm

It works in IE9 and Google Chrome but I can't get it work in Firefox! Any suggestions?

Here is my test file:

http://mngaragesales.com/tropetest3.shtml

Thanks!

djr33
12-09-2011, 08:52 PM
HTML and Javascript don't have convenient ways to play sounds in all browsers. There are a few limited methods, but they'll only work in some browsers.

Javascript can use an onclick event, though. But you'll need something else to play the sound.

My recommendation is the following script:
http://www.schillmania.com/projects/soundmanager2/

suzzy
12-09-2011, 11:29 PM
Thanks Daniel, I'll look into that program.

I was able to fix the issue by saving the sound file as an .ogg and having that as an option for the browser to choose from. I didn't realize that mp3s wouldn't work in that script in Firefox. :)

djr33
12-10-2011, 12:02 AM
Having some sort of script that allows multiple formats will be helpful to you. Not all browsers can play all formats, and in fact there isn't always overlap for even one format. A script like soundmanager2 will conditionally choose one format for the current browser (eg, using flash, or using the HTML5 audio element) and save you work.