Greetings
I’m using a script to display rotating youtube videos in the sidebar of my invision board. The problem is that the random thing causes the same video to be repeated twice in row at times. I need to add a cookie so that no video gets displayed to the same user until all other videos have been displayed. I also look for adding – if possible – a line to count the videos, so that I don’t have to edit the number each time I add more videos..
Can someone please help me with that?
Here is the script I’m using:
[code=html]<script type=”text/javascript”>
function rand ( n )
{
return ( Math.floor ( Math.random ( ) * n + 1 ) );
}
// Store youtube [CHANGES NEEDED IN THE 3 URLS BELOW]
var vids = new Array ( );
vids[0] = “http://www.youtube.com/embed/aF4M0JtoIPk”;
vids[1] = “http://www.youtube.com/embed/KMxWhovYfzc”;
vids[2] = “http://www.youtube.com/embed/ffZUWZQE0EU”;
vids[3] = “http://www.youtube.com/embed/povUgNcf4aI”;
vids[4] = “http://www.youtube.com/embed/Zs9u7X0rrd0”;
vids[5] = “http://www.youtube.com/embed/D4L0KpqNzkU”;
vids[6] = “http://www.youtube.com/embed/Mfdnb0Q7IBM”;
vids[7] = “http://www.youtube.com/embed/D-JJpPBjQwI”;
vids[8] = “http://www.youtube.com/embed/kn5NSCJ2tiI”;
vids[9] = “http://www.youtube.com/embed/BDxDMCWShds”;
// Pick a random video from the list
function pick_vid ( )
{
var numberOfImages = 10; //[CHANGE THE 3 TO THE TOTAL NUMBER OF VIDS YOU USE]
var num = rand(numberOfImages)-1;
document.getElementById(“random_youtube_vid”).src = vids[num];
}
</script>
<script type=”text/javascript”>window.onload=pick_vid;</script>
<p style=”padding: 6px 0;text-align:center”>
<iframe id=”random_youtube_vid” width=”250″ height=”200″ frameborder=”0″ allowfullscreen></iframe>
</p>
I found another scripts that could be of help, as it uses div and cookies..
But its for general display of anything. It doesn’t have the iframe for the video. I don’t know how to merge both scripts. Or perhaps you have better ideas..
[code=html]<div id=”randomdiv1″ style=”display:none;”>
[content for div 1]
</div>
<div id=”randomdiv2″ style=”display:none;”>
[content for div 2]
</div>
<div id=”randomdiv3″ style=”display:none;”>
[content for div 3]
</div>
<script type=”text/javascript” language=”JavaScript”><!–
/* Random Div Display
Version 1.0
March 9, 2009
Will Bontrager
http://www.willmaster.com/
Copyright 2009 Bontrager Connection, LLC
For information about implementing this software, see the article at
http://www.willmaster.com/library/javascript/random-div-display.php
*/
// One place to customize:
//
// Type the number of div containers to randomly display.
NumberOfDivsToRandomDisplay = 3;
// No other customizations required.
////////////////////////////////////
var CookieName = ‘DivRamdomValueCookie’;
function DisplayRandomDiv() {
var r = Math.ceil(Math.random() * NumberOfDivsToRandomDisplay);
if(NumberOfDivsToRandomDisplay > 1) {
var ck = 0;
var cookiebegin = document.cookie.indexOf(CookieName + “=”);
if(cookiebegin > -1) {
cookiebegin += 1 + CookieName.length;
cookieend = document.cookie.indexOf(“;”,cookiebegin);
if(cookieend < cookiebegin) { cookieend = document.cookie.length; }
ck = parseInt(document.cookie.substring(cookiebegin,cookieend));
}
while(r == ck) { r = Math.ceil(Math.random() * NumberOfDivsToRandomDisplay); }
document.cookie = CookieName + “=” + r;
}
for( var i=1; i<=NumberOfDivsToRandomDisplay; i++) {
document.getElementById(“randomdiv”+i).style.display=”none”;
}
document.getElementById(“randomdiv”+r).style.display=”block”;
}
DisplayRandomDiv();
//–></script>
Many thanks in advance.