0

I want a script to change the background music randomly

this is the code I tried , but it doesn't work:

<!DOCTYPE html>
<html>
<body>
<script>
var BGM = [];
BGM[0] = "BGM/BGM1.mp3";
BGM[1] = "BGM/BGM2.mp3";
BGM[2] = "BGM/BGM3.mp3";
BGM[3] = "BGM/BGM4.mp3";
BGM[4] = "BGM/BGM5.mp3";
BGM[5] = "BGM/BGM5.mp6";

var idx = Math.round( Math.random() * 5 );

document.write("<embed src='"+BGM[idx]+"' autostart=true hidden=true         loop=true'>); 

</script>

</body>
</html>

What have I done wrong?

JSDBroughton
  • 3,966
  • 4
  • 32
  • 52
rico chu
  • 69
  • 1
  • 10

1 Answers1

1

Looks like you're missing a closing quote on your document.write call. Try:

document.write("<embed src='"+BGM[idx]+"' autostart=true hidden=true         loop=true'>"); 

Example: http://codepen.io/JasonGraham/pen/WxbZZo

Jason Graham
  • 904
  • 6
  • 12