Author Topic: Load A Random Video on Page Load with JavaScript  (Read 292 times)

0 Members and 1 Guest are viewing this topic.

Offline ebenzunlimited

  • Lead Admin
  • *****
Load A Random Video on Page Load with JavaScript
« on: February 05, 2014, 02:06:59 PM »
If you want to load different videos randomly on page refresh, this code may prove helpful.

Code: [Select]
<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>
« Last Edit: February 05, 2014, 06:44:26 PM by obasimiracle »
Insane I Lived♥ Sane I Died♥<img src='http://i253.photobucket.com/albums/hh74/reallytired2/family%20pictures/buckeye%20stuff/clap.gif' />

 

* Post Updates

Inside: 3P Techies Blog

* Newest Techies

Get Updates


Sign up to get latest updates delivered to your inbox. No Spam, We Promise!

Get Hosting!

a Faster web hosting service

Copyright 3rd Planet Techies. All rights Reserved.

Top || Mobile ||Advertise || Contact Us || Privacy