PDA

View Full Version : Placing Sound on Links ?



MystMun
01-09-2005, 02:33 AM
I want a sound effect to play when a link is clicked upon. Does anyone know the code for this?

theprincessbride
01-10-2005, 12:26 AM
Javascript; Play Sound Files on Mouseover of Link
There are 2 ways to play sound on mouseover. This first way seems to be more reliable than the javascript version

This is a great javascript which allows you to play different sounds as your visitors runs their mouse over each link or music on mouseover of an image.


Sitemap Run your mouse over the links for an example!

Java Scripts

Paint Shop Pro Tutorials

STEP 1

Place this first part of the script into the body (content) of your page which preloads the sound files.

Substitute the sound files for your own, below the fifth line. The first sound file will be 0, the second 1, the third 2 and so on. You can add as many different sound files as you have links.

<!-- --><bgsound id="soundfiles">
<script language="JavaScript"><!--
// Sound on Mouseover javascript supplied by http://www.hypergurl.com

var aySound = new Array();
// PLACE YOUR SOUND FILES BELOW
aySound[0] = "CHORD.wav";
aySound[1] = "DING.wav";
aySound[2] = "CHORD.wav";
// Don't alter anything below this line

IE = (navigator.appVersion.indexOf("MSIE")!=-1 && document.all)? 1:0;
NS = (navigator.appName=="Netscape" && navigator.plugins["LiveAudio"])? 1:0;
ver4 = IE||NS? 1:0;
onload=auPreload;

function auPreload() {
if (!ver4) return;
if (NS) auEmb = new Layer(0,window);
else {
Str = "<DIV ID='auEmb' STYLE='position:absolute;'></DIV>";
document.body.insertAdjacentHTML("BeforeEnd",Str);
}
var Str = '';
for (i=0;i<aySound.length;i++)
Str += "<EMBED SRC='"+aySound[i]+"' AUTOSTART='FALSE' HIDDEN='TRUE'>"
if (IE) auEmb.innerHTML = Str;
else {
auEmb.document.open();
auEmb.document.write(Str);
auEmb.document.close();
}
auCon = IE? document.all.soundfiles:auEmb;
auCon.control = auCtrl;
}
function auCtrl(whSound,play) {
if (IE) this.src = play? aySound[whSound]:'';
else eval("this.document.embeds[whSound]." + (play? "play()":"stop()"))
}
function playSound(whSound) { if (window.auCon) auCon.control(whSound,true); }
function stopSound(whSound) { if (window.auCon) auCon.control(whSound,false); }
//--></script>
<!-- -->
STEP 2

Simply add this code into each link, changing the number of the mouseover event to correspond to your sound file.

onMouseOver="playSound(0)" onMouseOut="stopSound(0)"

Your link code should look like this;

<a href="http://www.yourlink.com" onMouseOver="playSound(0)" onMouseOut="stopSound(0)">YOUR LINK</a>

Want to play music on mouseover of an image or object?





The first part of the javascript will be the same. The html code on the image or object will be similar to this:

<img src="yourimage.gif" onMouseOver="playSound(3)" onMouseOut="stopSound(3)">

MystMun
01-12-2005, 03:57 PM
Thank you very much! It worked great! =)

potterspread
02-08-2007, 04:54 PM
Anyone know how to alter this code so that it works with Macintosh browsers?

Thanks in advance -

mike

djr33
02-08-2007, 10:49 PM
FF for mac should be just fine. Safari may not. I'm not really sure.
Does it just do nothing?

Shotgun Ninja
02-09-2007, 02:30 PM
Whoa, that's quite a bit of code for a simple sound bit.

potterspread
02-13-2007, 05:58 AM
Correct - it doesn't do anything..

TheBigT
02-13-2007, 09:21 PM
I am sorry, I do not know much Javascript, so I have a question.

Does the script preload the .wav files or does it retrieve them after the mouseover event?