MouseOver sound using JavaScript

Hi,

This code snippets will give you additional impact for your programs.
If you mouse over the icon you can hear the sound.


<script LANGUAGE="JavaScript"><!--
// Preload and play audio files with event handler (MouseOver sound)


var aySound = new Array();

aySound[0] = "laser.wav";


document.write('<BGSOUND ID="auIEContainer">')
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;iStr += "if (IE) auEmb.innerHTML = Str;
else {
auEmb.document.open();
auEmb.document.write(Str);
auEmb.document.close();
}
auCon = IE? document.all.auIEContainer: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>



Then You have to add the sound files in array for Preload actions:

aySound[0]="YourSoundFile.wav";
aySound[1]="AnotherSoundFile.wav";
aySound[2]="EvenAnotherOne.wav";
etc.,



Then you have to do the code with Event Handler,
MouseOver/MouseOut

<A HREF="YourPage.html" onMouseOver="playSound(0)" onMouseOut="stopSound(0)">Move mouse over to play sound</A>


In OnClick Sound:

<A HREF="javascript:playSound(0);">Click here to play sound</A>


If you are performing operation by clicking the Buttons means,

<INPUT TYPE="BUTTON" VALUE="Click me!" onClick="playSound(0)">


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: