Basic Background Music

Most people like to browse the Web in peaceful silence. That means no trance-hypno-ambient background tracks, no strange disco beats, and no sudden cymbal crashes. This aversion to noise may be due to the fact that something like 98 percent of all web browsing takes place on company time.

But if you like to startle and annoy people, or if you’re absolutely convinced that your web audience really does want some funky beats, keep reading to bring on the background music.

The <embed> Element

Although the HTML standard doesn’t support background music, almost all browsers support the <embed> element, first pioneered by Netscape in the early days of the Web. You can put the <embed> element anywhere on your page. Here’s a basic page that uses it to play background music:

<!DOCTYPE html>

<html>
<head>
  <title>Background Music</title>
</head>
<body>
<h1>Automatic, Unsolicited Music</h1>
<p>The music now blaring from your speakers is
Scarlatti's first sonata (K. 500).
I hope you didn't tell your colleagues you were working!</p>
<embed src="scarlatti.mp3" type="audio/mpeg" />
</body>
</html>

The <embed> element gives you a slew of options for playback control. If you use the element without specifying any of them (as in the previous example), your visitors see a page like the one shown in Figure 17-3 and hear its audio file automatically.

Music playback isn’t always this seamless, however. Because every browser handles embedded music a little differently, you can run into ...

Get Creating a Website: The Missing Manual, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.