I put the following JS directly on a site of my WordPress blog. It should play one of the two audio files randomly if you click on a button.
My question is:
- Is there anything wrong / missing with the JS
- How can I get it started by a button?
<script>
var playlist = Array();
playlist.push("http://.../wp-content/uploads/2015/03/xxx.mp3");
playlist.push("...wp-content/uploads/2015/03/10-Good-Evening.mp3");
console.log(getSong()); // Here's your song.
document.getElementById("play").addEventListener("click", playSong);
function playSong(){
var audio = new Audio(getSong());
audio.play();
}
function getSong() {
return playlist[Math.floor(Math.random() * playlist.length)];
}
</script>