First things first - the embedded scripting on the link tag is going to give you problems, so let’s deal with that first.
<!--button/link-->
<a href="#" class="nextPage">Next page</a>
<!--/button/link-->
<!--[ At the end of the body section of the HTML ]-->
<script type="text/javascript">
...
</script>
That’s much better. Now your scripting can easily access the next page link, using document.querySelector(‘.nextpage’) or variations thereof.
If you know how long the audio is, such as 1000 milliseconds, then you could do something like this:
function playRedirect(url) {
play_single_sound();
setTimeout(function () {
location.href = url;
}, 1000);
}
but what would be better is to find out how long the audio is, and wait for it to finish playing.
Using setInterval we can check for the end of the audio being played, which can be done 4 times a second (250 milliseconds), which doesn’t result in any noticeable performance loss of the computer, and is speedy enough to not be noticied by the person using it. We can then load the new page when the end stops increasing. For flexibility, the audio target and the url have been given as variables, so that this code can be used in multiple situations.
setInterval(function () {
var end = document.getElementById(target).played.end(0);
if (end > time) {
time = end;
} else {
newPage(url);
}
}, 250);
For the sake of completion, here’s the new link and the whole script, as a working example.
<audio id="audiotag" src="Brave-Roar.mp3" autobuffer="autobuffer"></audio>
function play_single_sound(target) {
document.getElementById(target).play();
}
function newPage(url) {
location.href = url;
}
function playThenRedirectTo(audioTarget, url) {
var time = 0;
play_single_sound(audioTarget);
setInterval(function () {
var end = document.getElementById(audioTarget).played.end(0);
if (end > time) {
time = end;
} else {
newPage(url);
}
}, 250);
}
document.querySelector('.nextPage').onclick = function () {
playThenRedirectTo('audiotag', 'http://www.google.com/');
return false;
}
As a side-note - I had to play Brave Roar many times through as a part of testing this. I am not unpleased.