Browse DevX
Sign up for e-mail newsletters from DevX


Inserting Audio : Page 5

In this section, you'll learn how to insert audio into your site.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Adding Sound via Scripting
JavaScript your sound for a whole new level of sound control for your site.

Giving Sound to Links
You can use scripts to control sound in a number of different ways. One thing people often want to do is have a transition sound, a sound that plays when they click on a link to another page.

Here's how to do this:

First, at the top of your page between the <script> and </script> tags, you'll add a function that tests to see what browser the reader is using and, if he or she is using IE4 or later, creates a function that we named "clickSounder." It also calls the sound file you'll be using; by calling for the sound here, it will play when the page first loads and remain the the browser cache, so that it needn't be reloaded when the reader clicks on a link. That bit of scripting looks like this:

ie4 = ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4 ));
function clickSounder(){
  if (ie4){


This function works only with Internet Explorer, but it won't break on Netscape. It's best used in a framed table-of-contents type file.

Next, each time you add a link that plays the sound, you'll need to add a snippet of JavaScript to your anchor tag, like this:

<a href="06-scripting.html" onClick="clickSounder();">Scripting Sound</a> <bgsound id="clickSnd" src="/developer/audioz/AZsounds/azBut&roll/jRdrm1.mid" volume=-1500>

  • The anchor tag calls the linked file as usual. In this example the linked file is called "06-scripting.html."
  • After the URL, the onClick eventhandler tells the browser that when a reader clicks on this link, it should use the function named clickSounder.
  • Then you complete the anchor tag as usual, adding the linked text and the end anchor tag.
  • Immediately following the anchor tag, you'll need to specify the sound that should play, using the bgsound tag. The id switch value is "clickSnd," the name we gave the sound we loaded as part of the script at the beginning of the page. The src switch specifies the URL of the sound. The volume switch says how loud to play the sound—values range from -1500 to 1500.

Playing Sounds When a Page Loads
Another thing you might want to do is have your sound to start playing automatically when the page loads. You can do this by adding a little JavaScript to your body tag, like this: onLoad="window.location='url of soundfile."

You can determine the volume of your onclick sound—anywhere from -10000 to 0, with 0 being the loudest.

The sound will play once when page is loaded, which causes the sound file to cache. To improve functionality, the <bgsound> tag should be last tag in "body."

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date