What's new

Load A Random Video on Page Load with JavaScript

ebenzunlimited

Moderator
If you want to load different videos randomly on page refresh, this code may prove helpful.

Code:
<script type=&#34;text/javascript&#34;>
// Create an array with all the file names
var urls = &#91;
  &#34;file_1.swf&#34;,
  &#34;file_2.swf&#34;
&#93;

// Create a random number between 0 and the length of the array
var randNumber = Math.floor&#40;Math.random&#40;&#41; * urls.length&#41;;

// There&#39;s a very tiny chance that the random number is too big
randNumber -= &#40;randNumber == urls.length&#41;?1&#58;0;

// Select a filename from the array
var randomFile = urls&#91;randNumber&#93;;

// I don&#39;t usually recommend using document.write&#40;&#41;, for many reasons.
// But since this is supposed to load as soon as the page does, it&#39;s OK.

document.write&#40;&#34;<object type=\&#34;application/x-shockwave-flash\&#34; data=\&#34;&#34; + radomFile + &#34;\&#34; width=\&#34;500\&#34; height=\&#34;440\&#34;>&#34;&#41;;
document.write&#40;&#34;<param name=\&#34;movie\&#34; value=\&#34;&#34; + randomFile + &#34;\&#34;>&#34;&#41;;
document.write&#40;&#34;</object>&#34;&#41;;
</script>
 
Top