简体   繁体   中英

Play sound when Link Clicked

I have written a small web application but am missing some cool functionality. I want to play a sound when the user clicks the link, but after playing the sound continues to navigate to the URL the link points to.

Any ideas?

While the pursuit of coolness is a worthy ideal, I would strongly recommend against playing a sound when a user clicks a link (or at any time!). As both a user and a web developer this would startle me right to the back button. Only several minutes later would I be witty enough to think that I just time traveled to the mid 1990's

EDIT

unless you're going for something retro, like this: (which i highly suspect you aren't)

http://www2.warnerbros.com/spacejam/movie/jam.htm

EDIT II

Hmm... well I guess a being a game changes things a little. I would recommend binding the click event with jquery to append a sound file to the dom, something like this:

$('a').click(function(){
    $('body').append('<embed src="/path/to/your/sound.wav" autostart="true" hidden="true" loop="false">');
    pause(1000);   //Number of milliseconds to pause
    window.location = http://www.asdf.com //new url to go to
});
function pause(milliseconds) {
    var dt = new Date();
    while ((new Date()) - dt <= milliseconds) { /* Do nothing */ }
}

Attach an onclick handler to the link with JavaScript. This function will need to:

  1. Inject or activate a Flash movie or other object capable of playing the audio. JavaScript has no audio-generation abilities itself.

  2. Start a timer to redirect to the link's original location after a suitable delay to load and play the sound. Use window.location to redirect the browser to the URL.

  3. return false to stop the default click action (leaving the page) from occurring too fast for the sound to play.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM