Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

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

irt.org | Knowledge Base | JavaScript | Sound | Q453 [ previous next ]

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

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>

Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.