|
JavaScript Random Sounds
This Web page includes a JavaScript that plays a randomly selected sound file each time you load the page. Here are the instructions:
<SCRIPT LANGUAGE="JavaScript">
<!--
// This function plays a random sound
function PlaySound() {
// Use the following variable to
// specify the number of sound files
var NumberOfSounds = 6
// This variable holds the array of
// sound files to choose from
var sounds = new BuildArray(NumberOfSounds)
// This variable specifies the URL of the
// directory that contains the sound files
var soundDir = "http://www.mcfedries.com/toys/"
// Use the following variables to
// specify the sound files:
sounds[1] = soundDir + "applause.au"
sounds[2] = soundDir + "breaking_glass.au"
sounds[3] = soundDir + "chirp.au"
sounds[4] = soundDir + "doorbell.au"
sounds[5] = soundDir + "gong.au"
sounds[6] = soundDir + "cowbell.au"
// Use the time (i.e., seconds) to get a random number
var d = new Date()
var secs = d.getSeconds()
var rnd = (secs % sounds.length) + 1
snd = sounds[rnd]
// Use "href" method to play the sound
// (as though the user clicked it)
window.location.href=snd
}
// This function is used to create a new array
function BuildArray(size){
this.length = size
for (var i = 1; i <= size; i++){
this[i] = null}
return this
}
//-->
</SCRIPT>
Copyright © 1995-2008 Paul McFedries and Logophilia Limited |