Wednesday, May 8, 2013

Simple way to play sound onMouseover event

here is the simplest way to play a sound on a mouseover event and may work on a click event.
Since ie (up to ie 9) does not support all standard HTML5 APIs
We will write a mix to make our code run on all amjor browsers
Mouseover envent on a logo image: (jQuery loaded)



<script language="javascript" type="text/javascript">
jQuery(document).ready(function() {
  jQuery("#website-logo").hover(function() {
  if (jQuery.browser.msie) { /* For ie*/
    document.all.sound.src = '/sounds/dog.wav';
  } else /*Other browsers*/
playSound('/sounds/dog.wav');
  });
});
 function playSound(soundfile) {
var snd = new Audio(soundfile);/*HTML5 Api */
snd.play();
 }
 </script>

<bgsound id="sound" /> <!-- needed in ie case -->

<div id="logo-div">
<a href="/"><img src="/img/logo.png" id="website-logo" alt="Word Hard And Fun logo"></a>
</div>


No comments:

Post a Comment