Hey, I am trying to get a sound to play when the user moves the mouse over on of my links. I don't know javascript, I just copied this code from a book, so it's probably something simple. Here is my code:

<script language="JavaScript" type="text/javascript">
      function playSound()
      {
      src="misc020.wav";
      }
</script>

<A HREF="" onMouseOver="playSound()">Home</A></td>

Dani AI

Generated

Your original function only assigns a variable called src — it never creates an audio element or tells the browser to play anything, so nothing happens. was right to flag that. The simplest modern fix is to use the HTML5 audio API (either an <audio> element or new Audio(...)) and call play(); note that many browsers enforce autoplay/user-gesture rules, so a programmatic play() can be blocked unless it happens in response to a user interaction. (developer.mozilla.org)

A minimal, reliable pattern: create a single Audio object, preload it, and trigger play() from a real user gesture (click/pointerdown). Reset currentTime if you want the effect to restart on repeated activations and catch play() rejections so you don't get uncaught Promise errors.

// create once (on page load)
const sfx = new Audio('sound.mp3');
sfx.preload = 'auto';

// play on a user gesture (pointerdown/click)
document.querySelector('a.myLink').addEventListener('pointerdown', (e) => {
  sfx.currentTime = 0;
  sfx.play().catch(err => console.warn('play blocked or failed:', err));
});

If you really want hover behavior, prefer pointerdown/click or initialize audio on the first real gesture and then allow mouseenter to play effects — many browsers won't treat plain mouseover as a reliable user gesture. Also consider short, muted fallback or using muted briefly to detect autoplay policy before enabling audible effects. (developer.mozilla.org)

Troubleshooting checklist: verify the audio URL with the Network panel (200/206 responses), confirm the server sends correct MIME types and supports range requests, and provide multiple formats (MP3/AAC and Ogg/WebM) for compatibility across browsers. For advanced mixing, synthesis, or very low-latency effects consider the Web Audio API and create/resume the AudioContext inside a user gesture. These are the common causes when sound "does not play" after wiring up code. (developer.mozilla.org)

Notes tied to the thread: ’s SoundManager suggestion (and ’s follow-up) solved cross-browser/plugin issues historically, and ’s/javascriptkit pointers show legacy examples — both are fine if you need robust fallbacks. For most modern projects the built-in Audio/WebAudio approach above is simpler and avoids plugins.

Recommended Answers

All 6 Replies

That's weird looking script. I can't see any way that it could work - in that context; 'src' wont relate to anything useful.

This page has some example code (lots of examples); it won't work for me; but I'm not on a computer with many media plugins:

Hope that helps some...

the java script doesnt work ... can v include some other class or script to run music in web page or ... some other thing to do ??????

That's weird looking script. I can't see any way that it could work - in that context; 'src' wont relate to anything useful.

This page has some example code (lots of examples); it won't work for me; but I'm not on a computer with many media plugins:

Hope that helps some...

thanks I used #4 on that list, and it works, check it out, the links on the left side of the page are where it is designed to play:

pretty sweet8, even if my IE blocked the WMP active x to start with ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.