Idocs Guide to HTML
music: "Hazy Shade of Winter" by Paul Simon

Background Sounds

A "background sound" is a sound that starts to play automatically when the web page is loaded.

Before you go any further, think hard: Do you really want to do this? Background sounds have a way of annoying people. There someone is, happily surfing the net, when suddenly their computer starts playing music for everyone to hear, perhaps everyone in the office area, who are now finding out that our formerly happy surfer isn't really working. That's not to say that background sounds are never a good idea... you may have noticed that we use one on this page. It just seemed appropriate for a page about sounds on web pages.

Now, assuming you've thought through the consequences of your actions, let's talk about how to put a background sound on your page. Unfortunately the browser industry and standards committees have not settled on a standard way of accomplishing this. Netscape allows background sounds through use of the <EMBED ...> tag. MSIE, Mosaic, and several other browsers use the <BGSOUND ...> tag. You might at this point ask why you can't just use both and let each interpret its preferred tag. The problem is that MSIE sometimes interprets the <EMBED ...> tag in addition to <BGSOUND ...> (depending on the particular installation) resulting in conflict and ugly error messages. This is the sort of thing that happens when there isn't a standard to follow.

With this lack-of-standards problem, the best to hope for is a situation that plays the sound in most situations, and in the other situations doesn't play the sound and doesn't give error messages. This can be accomplished using scripting, as in this example.

<SCRIPT TYPE="text/javascript">
var filename="hazy_shade_of_winter.mid";
if (navigator.appName == "Microsoft Internet Explorer")
    document.writeln ('<BGSOUND SRC="' + filename + '">');
else if (navigator.appName == "Netscape")
    document.writeln ('<EMBED SRC="' + filename + '" AUTOSTART=TRUE WIDTH=144 HEIGHT=60><P>');
// -->
<BGSOUND SRC="hazy_shade_of_winter.mid">

The code says this:

  1. If the current browser is MSIE then write out a <BGSOUND ...> tag.
  2. Else, if the current browser is Netscape then write out an <EMBED ...> tag.
  3. Then in the <NOSCRIPT> section, browsers that don't understand scripting will see the <BGSOUND ...> tag. Several browsers that don't understand scripting do understand <BGSOUND ...>. Also, browsers that have scripting turned off but do understand <BGSOUND ...> will also see this tag. Of course, browsers that have scripting turned off but don't understand <BGSOUND ...> will have no sound at all. Again, that's the price being paid for lack of standards. At least they won't fail with an ugly error message.
That's kind of complicated, isn't it? Well, that's the problem with this sort of kludge: you have to try to anticipate all the situations and avoid the nasty ones. Hopefully in the foreseeable future the browser makers will correct this problem. Copyright 1997-2002 Idocs Inc. Content in this guide is offered freely to the public under the terms of the Open Content License and the Open Publication License. Contents may be redistributed or republished freely under these terms so long as credit to the original creator and contributors is maintained.