javascript - Youtube playlist embedding with randomize -


for small video contest (most likes on facebook) trying embed youtube playlist inside of homepage. each video has button underneath.

now trying achieve is, every reload of page order of videos should change, chances equal.

so far, code (no php allowed because of ckeditor)

<script type="text/javascript"> google.setonloadcallback(function () { // setup click handler function loadvideo(video) {  $('#videos h3').text(video.title);  $('#youtubevideo').html([   '<iframe style="max-width:100%;" width="839" height="472" title="', video.title, '" src="', video.href, '" frameborder="0" allowfullscreen="true"></iframe>' ].join("")); return false; }  // feed info // setting max-results delivers maximum of 50, can remove default 25. use feed.setnumentries strip down range between 50 , 25. var feedurl = "http://gdata.youtube.com/feeds/api/playlists/5602ed8dd2643fc2?max-results=50"; new google.feeds.lookupfeed(feedurl, function (result) { if (result.error || !result.url) { $('#videocomm').hide(); return; } // feed items var feed = new google.feeds.feed(result.url); feed.setnumentries(50); feed.load(function (result) { // write out feed data var container = $(".youtubefeed"); //var totalcount = result.feed.entries.length; //alert (totalcount);  (var = 0; < result.feed.entries.length; i++) { var entry = result.feed.entries[i]; var vidhash = /=(.*)&(.*)$/.exec (entry.link)[1]; // uncomment , comment out item below if find rendering of player bit slow // container.append('<li><div><a href="http://www.youtube.com/v/'+vidhash+'&feature=youtube_gdata&rel=1" class="yt-vid-link" title="'+entry.title+'"><img src="http://img.youtube.com/vi/'+vidhash+'/2.jpg" /><br />'+entry.title+'</a></div></li>\n');  // comment item below , uncomment item above if find player swap slow   container.append('<li class="vid-list-item"><span class="yt-title">'+entry.title+'</span><a href="http://www.youtube.com/embed/'+vidhash+'" class="yt-vid-link" title="'+entry.title+'"><img src="http://img.youtube.com/vi/'+vidhash+'/2.jpg" /></a><iframe src="//www.facebook.com/plugins/like.php?href=https%3a%2f%2fwww.youtube.com/watch?v='+vidhash+'=&amp;send=false&amp;layout=button_count&amp;width=200&amp;show_faces=false&amp;font&amp;colorscheme=light&amp;action=like&amp;height=21&amp;appid=469231673121827" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:200px; height:21px;" allowtransparency="true"></iframe></li>\n'); } // load first video $(".yt-vid-link:first").each(function () { loadvideo(this); return false; }); // setup click handler videso $(".yt-vid-link").click(function () { loadvideo(this); return false; }); }); }); }); google.load("feeds", "1"); </script>  <div id="videos"> <div id="videocomm"> <h3>warte kurz, wir laden die videos....</h3>  <div id="youtubevideo">&nbsp;</div>  <div class="slider"> <div id="fragment-1"> <ul class="youtubefeed"> </ul> </div> </div> </div> </div> 

update: came with

function zufall() {  return (math.random() - math.random()); } result.feed.entries.sort(zufall);  

as far can see, can randomize result.feed.entries desired result.

you can use function it:

function fisheryates ( myarray ) {   var = myarray.length, j, tempi, tempj;   if ( === 0 ) return false;   while ( --i ) {      j = math.floor( math.random() * ( + 1 ) );      tempi = myarray[i];      tempj = myarray[j];      myarray[i] = tempj;      myarray[j] = tempi;    } } 

see stackoverflow answer.


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -