Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q453 How can I play a random midi file when the page opens?

You are here: irt.org | FAQ | JavaScript | Sound | Q453 [ previous next ]

Output the HTML using JavaScript. If the HTML looks like:

<EMBED SRC="sound1.mid" HIDDEN="TRUE" AUTOSTART="TRUE" LOOP="FALSE">

And if you've got midi files sound1.mid throught to sound9.mid, then all you need to do is create a random number between 1 and 9, and then combine that with the rest of the file name:

<HTML>
<HEAD>
<SCRIPT><!--
// The Central Randomizer 1.3 (C) 1997 by Paul Houle (houle@msc.cornell.edu)
// See:  http://www.msc.cornell.edu/~houle/JavaScript/randomizer.html

rnd.today=new Date();
rnd.seed=rnd.today.getTime();

function rnd() {
        rnd.seed = (rnd.seed*9301+49297) % 233280;
         return rnd.seed/(233280.0);
};

function rand(number) {
        return Math.ceil(rnd()*number);
};

// end central randomizer. -->
</SCRIPT>
</HEAD>

<BODY>

<SCRIPT LANGUAGE="JavaScript"><!--
var x = rand(9);

document.write('<EMBED SRC="sound' + x + '.mid" HIDDEN="TRUE" AUTOSTART="TRUE" LOOP="FALSE">');
//--></SCRIPT>

</BODY>
</HTML>

©2018 Martin Webb