#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2007
    Posts
    46
    Rep Power
    0

    Can't Get This Random Video Generator To Work


    Alright, so I just want an array with embed codes of YouTube videos and each pageload loads a different one from the video. Shouldn't be too complex yeah?

    Here's the code I'm using, but it won't work. Can someone help me out please?

    Code:
    <script type="text/javascript"> 
    
    var video = newArray(); 
    var select=0; 
    
    video[0] = "<iframe width="560" height="200" src="http://www.youtube.com/embed/7Am7i7uM9r0" frameborder="0" allowfullscreen></iframe>"; 
    
    video[1] = "<iframe width="560" height="200" src="http://www.youtube.com/embed/ceBXUyuv4Q0" frameborder="0" allowfullscreen></iframe>"; 
    
    video[2] = "HTML EMBED CODE HERE"; video[3] = "HTML EMBED CODE HERE"; 
    
    select = Math.floor(Math.random() * video.length); document.write(video[select]); 
    
    </script>
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,044
    Rep Power
    119
    I changed a few things; but it's less recursive this way.

    Code:
    <iframe id="my_video_player" width="560" height="200" frameborder="0" allowfullscreen></iframe>
    
    <script type="text/javascript"> 
    
    var video = new Array(); 
    video[0] = "http://www.youtube.com/embed/7Am7i7uM9r0";
    video[1] = "http://www.youtube.com/embed/ceBXUyuv4Q0";
    
    var maxVideo = video.length - 1;
    
    (function() {
    var selection = Math.round(Math.random() * maxVideo);
    document.getElementById("my_video_player").src = video[selection];
    })();
    
    </script>
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2007
    Posts
    46
    Rep Power
    0
    Not working man. Nothing shows up on the page.
  6. #4
  7. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,044
    Rep Power
    119
    Tested it, before I posted it... it works fine; copy and paste the code... just like I have it.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2007
    Posts
    46
    Rep Power
    0
    Done.

    Works just fine, apologies.

    Thanks mate.
  10. #6
  11. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,044
    Rep Power
    119
    No problem, glad you got it working.

    Comments on this post

    • Avtar9 agrees : Thanks for the help!
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2007
    Posts
    46
    Rep Power
    0
    Quick question though, how come the initial code wasn't working?
  14. #8
  15. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,044
    Rep Power
    119
    Originally Posted by Avtar9
    Quick question though, how come the initial code wasn't working?
    You had a few errors, that needed to be corrected; for your code to work. Here is the corrected version of your code:

    Code:
    <script type="text/javascript"> 
    
    var video = new Array(); // you had a syntax error... you had it like this: "newArray()"
    
    var select = 0;
    
    video[0] = '<iframe width="560" height="200" src="http://www.youtube.com/embed/7Am7i7uM9r0" frameborder="0" allowfullscreen></iframe>';
    
    video[1] = '<iframe width="560" height="200" src="http://www.youtube.com/embed/ceBXUyuv4Q0" frameborder="0" allowfullscreen></iframe>'; 
    
    video[2] = 'HTML EMBED CODE HERE 1';
    
    video[3] = 'HTML EMBED CODE HERE 2';
    
    video.length = video.length - 1; // video.length would be 4; so you have to subtract it by one... so you do not get an "undefined" error
    
    select = Math.floor(Math.random() * video.length);
    
    document.write(video[select]); 
    
    </script>
    Security Note: If I were you; I would not use document.write() no more then I had to though (I actually do not use it at all); as it can be a browser securtiy risk for XSS injections.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2007
    Posts
    46
    Rep Power
    0
    Originally Posted by web_loone08
    You had a few errors, that needed to be corrected; for your code to work. Here is the corrected version of your code:

    Code:
    <script type="text/javascript"> 
    
    var video = new Array(); // you had a syntax error... you had it like this: "newArray()"
    
    var select = 0;
    
    video[0] = '<iframe width="560" height="200" src="http://www.youtube.com/embed/7Am7i7uM9r0" frameborder="0" allowfullscreen></iframe>';
    
    video[1] = '<iframe width="560" height="200" src="http://www.youtube.com/embed/ceBXUyuv4Q0" frameborder="0" allowfullscreen></iframe>'; 
    
    video[2] = 'HTML EMBED CODE HERE 1';
    
    video[3] = 'HTML EMBED CODE HERE 2';
    
    video.length = video.length - 1; // video.length would be 4; so you have to subtract it by one... so you do not get an "undefined" error
    
    select = Math.floor(Math.random() * video.length);
    
    document.write(video[select]); 
    
    </script>
    Security Note: If I were you; I would not use document.write() no more then I had to though (I actually do not use it at all); as it can be a browser securtiy risk for XSS injections.
    Thanks man, losing the hang a little of JavaScript and because of C++ and what not I tend to always put the semicolon after statements, haha.

    Appreciate the help, that explains a lot. Was getting a little worried about what was wrong.

    A simple document.write like that could be an XSS loophole? How so?

IMN logo majestic logo threadwatch logo seochat tools logo