1

I have 170 HTML5 animations that need to be cycled randomly when a page loads.

I figure the easiest thing to do is randomly choose the JavaScript file name for the animation.

Can someone outline easiest way of doing that?

Thanks in advance.

EDIT: I realise that maybe the language I'm using is wrong - am learning as I go.

What I want to do is set the SRC attribute in a SCRIPT tag randomly from a list of 170 filenames.

So far, I've gleaned that it should be something like this,

<!--[if !IE]>--><script id="animation" src="" type="text/javascript">
</script> <!--<![endif]-->
<script type="text/javascript">
//Files
var filesArray = ['file1.js', 'file2.js'];
//src vars
var url = 'http://www.example.com/';
// Shuffle
var file = musicArray[Math.floor(Math.random() * myArray.length)];
//Set src
getElementById("animation").src = url + file;
</script>

But that's not working for me. Is it even possible to set the SRC attribute in this way?

r1853
  • 141
  • 1
  • 1
  • 9

1 Answers1

1

Just populate the fileNames array with the filenames and it will generate a random file name.

var fileNames = ['foo', 'bar', 'foobar'],
    fileLen   = fileNames.length;

setInterval(function() {
    console.log(fileNames[Math.floor(Math.random() * fileLen)]);
}, 2000);
arnolds
  • 778
  • 6
  • 12
  • Hi Arniekoz, Thanks a lot for the response. What I'm looking for is a way to populate FILE NAME HERE in this, `` I understand how to generate the file name randomly using JS, just not how I might then insert the output into the code to complete the javascript file request. – r1853 Feb 11 '15 at 15:07