Mathias Willner
Mathias Willner

Reputation: 1

onClick with multiple functions

how can i play a sound with onClick and open a new html site in the existing window? Sometimes it plays the sound, and sometimes the new site open.

Thanks for your help!

<div class="lowersitebuttons">
<button class="login" onClick="PlaySound(button1); window.location.href = '/Users/Mac/Dropbox/Sites/lcars1.html'">LOGIN</button>
<button class="cancel" onClick="PlaySound(button2)">CANCEL</button> 
</div>


    // JavaScript Document
    var button = new Audio();
    button1.src = "/Users/Mac/Dropbox/login/Computersounds/lcars.mp3";
    function PlaySound(button1) {
        button1.play();
    }

    button2.src = "/Users/Mac/Dropbox/login/Computersounds/alert23.mp3";
    function PlaySound(button2) {
        button2.play();
    }

Upvotes: 0

Views: 156

Answers (1)

flappix
flappix

Reputation: 2217

When the new URL is loaded your sound will stop playing. Instead you may want to wait until your sound was played to finish and load the new URL after that. To do so you can use the onend event of an Audio object.

button1.play();
button1.onend = function()
{
    window.location.href = "/Users/Mac/Dropbox/Sites/lcars1.html";
}

Upvotes: 3

Related Questions