Adding Sound via Scripting
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 ));
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.
<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 soundvalues range from -1500 to 1500.
Playing Sounds When a Page Loads
You can determine the volume of your onclick soundanywhere 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."