Mood music and background sounds

You can easily (maybe too easily) add a background sound that plays automatically when the page is opened.
    I like working with a short drumroll sound (you may have heard it when you got to this page) while I'm building the page because it gets very irritating after just a few repetitions.
    This reminds me that sounds can be more annoying than even blinking or scrolling text.
    If you do use a background sound, make certain that it is a background sound - at the very least cut the volume. A good level is where the listener almost has to strain to hear it. Try reducing the volume at least 50% for starters. If they like it, they can turn the volume up; if they don't, they can more easily ignore it instead of just leaving your site.
    Background sounds are one of those situations where you want to skip some of the JavaScript and just use a pragmatic approach. A purist's approach would be to use JavaScript to write the Netscape code for Netscape and the Explorer code for Explorer (after getting a value for the 'Browser' variable). But that doesn't always work.
    The problem is that Explorer 3.0 on the Mac, for instance, understands <BGSOUND> but doesn't understand JavaScript, so I put Explorer's <BGSOUND> instruction right after the <BODY> tag

<BGSOUND SRC='drumroll.wav'>

where Netscape politely ignores it and Explorer uses it. I can't do that with the <EMBED> tag because Explorer 3.0 Mac neither supports nor ignores it and inserts an ugly icon. So for Netscape add this JavaScript:

if (Browser== 'Netscape')
  document.write( '<EMBED SRC="drumroll.wav" AUTOSTART="TRUE" HIDDEN="TRUE">' );

    Netscape ignores the <BGSOUND> while Explorer ignores the JavaScript. And the sound plays (if the browser has the right plug-in).
    
You're fairly safe with this if your sound format is ulaw (.au), midi (.mid) or wave (.wav), but playing sounds depends on the user having the correct plug-ins and you might want to deliver these sounds 'on demand' by providing a link (and a warning) to the sound, rather than playing it in the background. To do that, put something like this on your page (no JavaScript needed, just click on the link):

Clicking here crashes Explorer 3.01 on my Mac, but others get to listen to a drumroll.

 

Please close the window when you're done.

© Hal Pawluk 1996-7  http://www.pawluk.com/pages/javatech/javascr.htm  pawluk@sprynet.com