Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2010
    Location
    Montreal, Quebec, Canada
    Posts
    18
    Thanks
    5
    Thanked 1 Time in 1 Post

    Link and onclick sound event

    Hello all, very specific problem for you all. I'm trying to add an onclick event to play a clicking sound when a link is clicked.

    Using this code http://webdesign.about.com/od/sound/...sound_oncl.htm

    Here's the javascript

    Code:
    <script language="javascript" type="text/javascript">
     function playSound(soundfile) {
     document.getElementById("dummy").innerHTML=
     "<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
     }
     </script>
    and the actual link

    Code:
    <a href="index.php" onclick="playSound('button.wav');">HOME</a>
    (yes I've put the dummy span in my body as well)

    I've followed all the instructions properly, and yes it works fine onclick when the href is blank only.

    for example:

    Code:
    <a href="#" onclick="playSound('button.wav');">HOME</a>
    But as soon as I point my link to another page (like it should be above, at index.php), the sound will just not play. Now I'm assuming it's because the page changes perhaps before the sound has the time to load and play? Or perhaps I'm just not getting something... Either way, a work around or an idea on what might be happening would be much appreciated. Thank you.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    I think you are right - as soon as the link is clicked then the page redirects and the playSound() function is not called.

    But why do you want to annoy your visitors in this way? It adds no value whatever to be required hear a sound when a link is clicked. Some people listen to music while they ae surfing - your click sounds will ensure they do not visit your site again.

    If you are really determined to do this, then do the redirect after the sound:-

    <a href="#" onclick="playSound('button.wav'); redirectToIndexPage()">HOME</a>

    or call redirectToindexPage() at the end of the playSound() function, perhaps with a short time delay.

    Code:
    <script type="text/javascript">
     function playSound(soundfile) {
     document.getElementById("dummy").innerHTML=
     "<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
    setTimeout(redirectToIndexPage, 2000);  // 2 seconds delay
     }
    
    function redirectToIndexPage() {
    window.location.href = "index.php";
    }
    
    </script>

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 04-29-2013 at 05:59 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    Drakius (04-30-2013)

  • #3
    New Coder
    Join Date
    Jul 2010
    Location
    Montreal, Quebec, Canada
    Posts
    18
    Thanks
    5
    Thanked 1 Time in 1 Post
    Yes, I assumed as much. And technically this is not a website for me. It was a request for a site that I finished sometime ago, they wanted me to add this, if it were up to me, I would leave it as is with no sounds.

    Usually am able to fix all my own problems, but I've never actually added onclick sounds, (because of the excellent reasoning that you gave and that I agree with) hence why I was a bit stumped looking for a workaround. I might just let them know it's a bad idea.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •