Embedding Sound in Web Pages, Part I: The BGSOUND Tag - Doc JavaScript
The BGSOUND Tag
Explorer's <BGSOUND>
tag is the simplest way to embed background sound in your page. It may be placed either within the HEAD
or the BODY
portion of the page. Remember to turn ON your browser's "Play sounds" option, under "Edit"/"Internet-Options"/"Advanced". Here is an html code that will
the Aladdin soundtrack ten times, every time you load it:
<HTML>
<HEAD>
<TITLE> testing </TITLE>
</HEAD>
<BODY>
<BGSOUND SRC="aladdin.mid" LOOP=10>
</BODY>
</HTML>
Similarly, the following HTML code will the X-files soundtrack five times:
<HTML>
<HEAD>
<TITLE> testing </TITLE>
</HEAD>
<BODY>
<BGSOUND SRC="xfiles.mid" LOOP=5>
</BODY>
</HTML>
Microsoft documents specify more parameters for the <BGSOUND>
tag than are currently supported. The following table lists the supported ones:
Parameter | Description |
ID="idvalue" | An identifier to be used for references in associated style sheet, hypertext links, and JavaScript code. Must begin with a letter and may include underscores. ID should be unique within the scope of the document. If duplicated, the collection of identical-ID elements may be referenced by their ordinal numbers. |
LOOP="n" | Specifies how many times the sound will loop. |
SRC="url" | Specifies the URL of the sound file. As shown above, sound files can be in any recognizable format (for example: "midi, wav, au" ) |
VOLUME="n" | Determines the loudness of the background sound. Values may range from -10,000 (weakest) to 0 (loudest). Not supported by the MAC. |
The following parameters are not supported:
Parameter | Description |
BALANCE="n" | Determines the division of sound power between the left and right speakers. Valid values are between -10,000 and +10,000, with 0 being perfect balance between the two. |
CLASS="classname" | The class of the tag to be used for assigning sub-class style sheet. |
TITLE="text" | Specifies text to accompany the sound. |
LANG="language" | Specifies the language to be used, in ISO standard abbreviation format. |
You can reference the <BGSOUND>
element in a JavaScript script. The sound-related properties are balance
, loop
, src
, and volume
. The src
and loop
properties are supported on both the PC and the Mac computers. The volume
property is supported only on PC.
To demonstrate the usage of JavaScript for controlling the background sound of a page, here is that will switch between the Aladding and X-files soundtracks every 30 seconds:
<HTML>
<HEAD>
<TITLE> testing </TITLE>
</HEAD>
<BODY>
<BGSOUND SRC="aladdin.mid" ID="changetrack">
<SCRIPT>
<!--
setInterval("switchMelody()",30000);
function switchMelody(){
if (document.all.changetrack.src == "aladdin.mid")
document.all.changetrack.src = "xfiles.mid"
else document.all.changetrack.src = "aladdin.mid";
}
//-->
</SCRIPT>
</BODY>
</HTML>
This code does not have any effect on the Mac computer. On Mac, <BGSOUND>
's properties cannot be changed by a JavaScript script in the middle of a soundtrack.
Created: May 31, 1998
Revised: May 31, 1998
URL: https://www.webreference.com/js/column20/bgsound.html