Click to See Complete Forum and Search --> : Help with on click play sound


Girlinprogress
10-16-2003, 12:50 PM
Ok, so here's the deal. I have a made an image map and have put this script in at the top.

<SCRIPT LANGUAGE = "JavaScript">
function PlaySound(){
window.location = "custom_include/cow2.wav"
}
</SCRIPT>

In my image map i have onclick="PlaySound()". It's not working.

I obviously am not a JS programmer. I am sure I need some otehr function defined above. Can anyone HELP!!!

Thanks so much!

Jona
10-16-2003, 02:14 PM
This would send the user to download the audio file, rather than play it from the browser.


<script type="text/javascript"><!--
function PlaySound()
{window.location.href="custom_include/cow2.wav";}
//--></script>


[J]ona

BestZest
10-16-2003, 03:33 PM
In your document put

<bgsound id="sound"></bgsound>

and change the function to:

function PlaySound(){
document.getElementById("sound").src="custom_include/cow2.wav"
}


Hope this is what you want
BestZest

Girlinprogress
10-16-2003, 03:46 PM
Thank you!! It works great.
But, can you answer me this question. Why do I need to use <bgsound id=>
Why can't I define the ID in the <a href> tag?

Just trying to understand.


Thanks again.

BestZest
10-16-2003, 03:53 PM
I'm not completley sure, but I guess the bgsound tag is just designated to handle sounds, where as a href handles links.

Does that help?

BestZest

Girlinprogress
10-16-2003, 03:59 PM
That makes some sense.

Now for the next problem.

It won't work in NEtscape.
Does that requre embedding it?

halifaxrick
10-16-2003, 04:01 PM
I used something like this in conjunction with an onmouseover, it should work with onclick too.

Although you have to be careful with what browser it works with

<script language="JavaScript"><!--
var can_play = false;

var mimetype = 'audio/midi';

if (navigator.mimeTypes) {
if (navigator.mimeTypes[mimetype] != null) {
if (navigator.mimeTypes[mimetype].enabledPlugin != null) {
can_play = true;
document.write('<embed src="sound.wav" hidden=true loop=false autostart=false>');
}
}
}

if (document.all) {
can_play = true;
document.write('<embed src="sound.wav" hidden=true loop=false autostart=false>');
}

function playSound() {
if (document.embeds && can_play) {
if (navigator.appName == 'Netscape')
{document.embeds[0].play();
document.images[0].src ='happy.gif';return true;}
else
{document.embeds[0].play();
document.images[0].src ='happy.gif';return true;}
}
}

function stopSound() {
if (document.embeds && can_play)
document.embeds[0].stop();
}
//--></script>

I just pulled this from an old page, there may be some small mistakes but it did work.