Helping you Build Your Own Social Network!

Faster, better and easier!

How would I display random Youtube videos in a widget

(4 posts) (2 voices)
  • Started 1 year ago by Shane_NY_777
  • Latest reply from Shane_NY_777

Tags:


  1. hello, I have 2 YouTube videos on the homepage of my site, but Now im thinking I would rather set it up with about 15 YouTube videos and have it randomly display only 1.

    I use the code (at bottom of this post) to display random affiliate ads. So I figured I could just edit that code and display a random YouTube vid instead, but it is not working. Does anyone know of another way to display random YouTube videos (that i supply) or know why the below code wont work for youtube but it will work for other links or images?

    Posted 1 year ago #
  2. It displayed the code wrong in first post. here it is:


    <SCRIPT language=JavaScript>
    <!--
    function random_imglink(){
    var mylink=new Array()
    //Add the links below include image src. backslash all quotation marks

    // ad 1
    mylink[1]="<a href~ "

    // ad 2
    mylink[2]="<a href~ "

    // ad 3
    mylink[3]="<a href~ "

    // Get random link
    var rl=Math.floor(Math.random()*mylink.length)
    if (rl==0)
    rl=1
    document.write(''+mylink[rl]+'')
    }
    random_imglink()
    //-->
    </SCRIPT>

    the above is the link code, and below is my attempt at using it for YouTube (i just used the same Youtube video until i figured out if it works.. which it doesnt.


    <SCRIPT language=JavaScript>
    <!--

    function random_imglink(){
    var mylink=new Array()
    //Add the links below. backslash all quotation marks
    // Kaspersky
    mylink[1]="<object type=\"application/x-shockwave-flash\" style=\"width:305px; height:250px;\" data=\"http://www.youtube.com/v/ZoMSS8FRoi8?rel=0&showsearch=0&showinfo=0\">
    <param name=\"movie\" value=\"http://www.youtube.com/v/ZoMSS8FRoi8?rel=0&showsearch=0&showinfo=0\" /></object>"

    mylink[2]="<object type=\"application/x-shockwave-flash\" style=\"width:305px; height:250px;\" data=\"http://www.youtube.com/v/ZoMSS8FRoi8?rel=0&showsearch=0&showinfo=0\">
    <param name=\"movie\" value=\"http://www.youtube.com/v/ZoMSS8FRoi8?rel=0&showsearch=0&showinfo=0\" /></object>"

    mylink[3]="<object type=\"application/x-shockwave-flash\" style=\"width:305px; height:250px;\" data=\"http://www.youtube.com/v/ZoMSS8FRoi8?rel=0&showsearch=0&showinfo=0\">
    <param name=\"movie\" value=\"http://www.youtube.com/v/ZoMSS8FRoi8?rel=0&showsearch=0&showinfo=0\" /></object>"

    // Get random link
    var rl=Math.floor(Math.random()*mylink.length)
    if (rl==0)
    rl=1
    document.write(''+mylink[rl]+'')
    }
    random_imglink()
    //-->
    </SCRIPT>

    Posted 1 year ago #
  3. Hi Shane,
    three things to note,
    1. start array index from zero for storing the videos
    2. remove the line if(r1==0)
    r1=1;

    3. call random_imagelink() where you want video to be.

    Your earlier code is good too but that decreases the chance of randomness slightly. Also,, you have all the thre videos same, change them to see the randomization.

    Posted 1 year ago #
  4. @brajesh - I feel like I say thank you to you every day! :) You've been very helpful man! I was trying for 4 hours to get that to work, and I couldn't, Thanks alot Brajesh!

    FYI - yeah i was putting the same video in just to see if i can get at least the video to show up, but it never even showed the vid until I did what you wrote: (1) deleted that line and (2) started the array at 0.

    Thanks!

    Posted 1 year ago #

Reply

You must log in to post.