(This is a duplicate post that I answered in two parts...try not to get too confused! The original thread started here.)
<hr>Hey Mark!!! There is no standard way of doing this in all browsers. Before you go any further, also consider whether you really want to do this. Background sounds have a way of annoying people. An example: someone is surfing the net, when suddenly their computer starts playing music for everyone in the office to hear, revealing that the happy surfer isn't really working.
The other item I have really wanted to accomplish is getting the "music" to play in Moz without having to actually click on the link. I know (or I think?) that it can be done, but my coding just isn't doing it! If you have any suggestions for that - I am all "ears"!! <img src=/S/grin.gif border=0 alt=grin width=15 height=15> <hr>
OK, so I've warned you - this is not to say that background sounds are a bad idea all of the time, but a lot of people do not like them. If you do not plan to provide a means of controlling whether the sound is playing or not, the annoyance is greater still...but here is something that will help.
To prevent error messages and make sure that it plays, you can use a little script that sniffs out the browser and inserts the proper HTML into the page as it is rendered. Thus:
<pre>< ! - -
if (navigator.appName == "Microsoft Internet Explorer")
document.writeln ('<BGSOUND SRC="' + filename + '">');
else if (navigator.appName == "Netscape")
document.writeln ('<EMBED SRC="' + filename + '" AUTOSTART=TRUE>');
// - - >
Please note that I had to chop up the HTML comment tags so that the Lounge would display this correctly.)
Here is what the code means:
Sorry it's not any cleaner than that; since there is no standard a kludge is all you are left with if you really want to embed sounds in a page.
- <LI>If the browser is Internet Explorer, write the BGSOUND tag
<LI>Else, if the browser identifies itself as Netscape, write out an EMBED tag.
<LI>The NOSCRIPT section is intended for browsers that do not understand script languages (admittedly few).