Hello--
It has been a few years since I posted anything here. I developed a web site that randomly played various MP3 files I stored. I am working to rewrite some of the code because it appears if my code to do this is no longer working. Can someone please look at the code and give me some suggestions as to what I need to change or fix to get this feature working again? I hope that I am doing this right. Thank you.

<SCRIPT LANGUAGE="Javascript">

   var sound1="Great Lakes Lighthouses/MP3 Files/Sound 1.MP3"
   var sound2="Great Lakes Lighthouses/MP3 Files/Sound 2.MP3"
   var sound3="Great Lakes Lighthouses/MP3 Files/Sound 3.MP3"
   var sound4="Great Lakes Lighthouses/MP3 Files/Sound 4.MP3"
   var sound5="Great Lakes Lighthouses/MP3 Files/Sound 5.MP3"
   var sound6="Great Lakes Lighthouses/MP3 Files/Sound 6.MP3"
   var sound7="Great Lakes Lighthouses/MP3 Files/Sound 7.MP3"
   var sound8="Great Lakes Lighthouses/MP3 Files/Sound 8.MP3"
   var sound9="Great Lakes Lighthouses/MP3 Files/Sound 9.MP3"
   var sound10="Great Lakes Lighthouses/MP3 Files/Sound 10.MP3"
   var sound11="Great Lakes Lighthouses/MP3 Files/Sound 11.MP3"
   var sound12="Great Lakes Lighthouses/MP3 Files/Sound 12.MP3"
   var sound13="Great Lakes Lighthouses/MP3 Files/Sound 13.MP3"
   var sound14="Great Lakes Lighthouses/MP3 Files/Sound 14.MP3"
   var sound15="Great Lakes Lighthouses/MP3 Files/Sound 15.MP3"
   var sound16="Great Lakes Lighthouses/MP3 Files/Sound 16.MP3"
   var sound17="Great Lakes Lighthouses/MP3 Files/Sound 17.MP3"
   var sound18="Great Lakes Lighthouses/MP3 Files/Sound 18.MP3"
   var sound19="Great Lakes Lighthouses/MP3 Files/Sound 19.MP3"
   var sound20="Great Lakes Lighthouses/MP3 Files/Sound 20.MP3"
   var x=Math.round(Math.random()*19)

   if (x==0) x=sound1
    else if (x==1)  x=sound2
    else if (x==2)  x=sound3
    else if (x==3)  x=sound4
    else if (x==4)  x=sound5
    else if (x==5)  x=sound6
    else if (x==6)  x=sound7
    else if (x==7)  x=sound8
    else if (x==8)  x=sound9
    else if (x==9)  x=sound10
    else if (x==10) x=sound11
    else if (x==11) x=sound12
    else if (x==12) x=sound13
    else if (x==13) x=sound14
    else if (x==14) x=sound15
    else if (x==15) x=sound16
    else if (x==16) x=sound17
    else if (x==17) x=sound18
    else if (x==18) x=sound19
    else x=sound20

if (navigator.appName=="Microsoft Internet Explorer")
document.write('<bgsound src='+'"'+x+'"'+' loop="2">')
else 
   document.write('<embed src='+'"'+x+'"'+'hidden="true" border="0" width="20" height="20" autostart="true" loop="true">')

  </script>

Dani AI

Generated

Short diagnosis and why this stopped working: the old approach that injects <bgsound>/<embed> via document.write relied on deprecated, vendor-specific tags and on browsers that allowed autoplay without a user gesture. Modern browsers treat those tags as nonstandard or ignore them, and they block autoplaying audio unless the user has interacted or the audio is muted. This combination explains why code that once worked now fails silently. (webplatform.github.io)

Recommended modern approach: replace the old technique with the HTML5 audio API (either a visible <audio> element or an Audio object), keep a simple array of track URLs, pick a random index with Math.floor(Math.random() * tracks.length), set audio.src, and call audio.play() while handling the promise rejection to present a play control when autoplay is blocked. Example pattern (minimal):

const tracks = ['sounds/track1.mp3','sounds/track2.mp3'];
const audio = new Audio();
function playRandom() {
  audio.src = tracks[Math.floor(Math.random()*tracks.length)];
  audio.play().catch(err => {
    console.log('Autoplay prevented; show a Play button instead', err);
  });
}

This uses the modern media model and avoids obsolete tags. (developer.mozilla.org)

Quick debugging checklist (most common causes): confirm a single MP3 URL opens directly in the browser (no 404), check DevTools Network → Response Headers → Content-Type (the server should send audio/mpeg for .mp3), and avoid or URL-encode spaces/special characters in filenames (or rename files to use hyphens/lowercase). Also remember case-sensitivity on Linux hosts (Sound1.MP3 vs sound1.mp3). Use encodeURIComponent or create filenames without spaces when building URLs programmatically. (developer.mozilla.org)

Practical notes and UX: because autoplay is often blocked, provide a visible play/unmute control and check the play() promise to fall back gracefully (show a button or message when a NotAllowedError occurs). ’s detection idea improved compatibility for old embed/bgsound code, but migrating to the HTML5 audio model is the more robust, future-proof solution. For further troubleshooting, include the page’s console errors, a direct MP3 URL, and the server type when posting the full page code. (developers.google.com)

Recommended Answers

All 4 Replies

/unknown/i.test(document.createElement('bgsound')) ?
document.write("<embed src='"+x+"' hidden autostart loop>") :
document.write("<bgsound src='"+x+"' loop>");

I am still a bit confused..Do I replace my code in lines 46-49 with this or what? Can you please give me a better explanation?

commented: We can all see that +0

Yes, it's a an obvious replacement of your old long working but recently failing part of the script.

And thank you for downvoting. My fault...

This is still not working..should I repost this question with my complete code rather than the short piece I thought was needed to solve this problem?

commented: it is working -2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.