A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: javascript for random movie onLoad

  1. #1
    Junior Member
    Join Date
    Apr 2003
    Posts
    2

    javascript for random movie onLoad

    does anyone have or know of a javaScript that will play random movies when a page loads?
    i can't do this with actionscript as i'm using movies created through oddcast/vhost (http://www.oddcast.com/oddcast/site/index.php) so i don't have access to the .fla as they are created dynamically online.
    any help would be appreciated.

  2. #2
    Senior Member catbert303's Avatar
    Join Date
    Aug 2001
    Location
    uk
    Posts
    11,222
    Hi welcome to flashkit,

    If you make an array containing the possible filenames of the movies, you can then pick one at random and use javascript to write the appropriate object and embed tags to the page,

    Code:
    <!-- in the body of the page where you want the movie to appear -->
    <script type="text/javascript">
    var movies = ["movie1.swf", "anotherfile.swf", "something.swf"]; // array containing filenames, add as many as you like
    var movie = movies[Math.floor(Math.random() * movies.length)]; // pick one item from the array at random
    // now write out the tags for the movie
    document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" width=550 height=300>');
    document.write('<param name=movie value="' + movie + '"> <param name=quality value=high> <param name=bgcolor value=#ffffff> <param name="menu" value="false">');
    document.write('<embed src="' + movie + '" quality=high bgcolor=#ffffff menu=false width=550 height=300 type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed>');
    document.write('</object>');
    </script>

  3. #3
    Junior Member
    Join Date
    Apr 2003
    Posts
    2
    thanks so much for your help!
    that's exactly what i needed!
    - andrew

  4. #4
    Junior Member
    Join Date
    Aug 2012
    Posts
    2
    Hi, I'm new here, so apologies in advance for what may be a dumb question in the wrong place.
    I am trying to do something similar to the above, but to play a random YouTube video from a list (of, say, 20 videos) rather than a swf file. In other words, on page load, I want one of my chosen YouTube videos to start playing, embedded on the page. I've just started getting to grips with HTML and Javascript (only just!), and I'm using Dreamweaver, if that's relevant.
    Any help greatly appreciated!
    Yours,
    Paul

  5. #5
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    I would rather use PHP to do that, but if you want to use the same method as the answer above, then just do the same with the YouTube embed code, which should look something like this:

    Code:
    <iframe width="420" height="315" src="http://www.youtube.com/embed/-UNFQ1o5brM" frameborder="0" allowfullscreen></iframe>
    your javascript code, in the form of the answer above, would look something like this:

    Code:
    <!-- in the body of the page where you want the movie to appear -->
    <script type="text/javascript">
    var movies = ["-UNFQ1o5brM", "xI3y5dJlKEU", "Xgoe8jaiwX4"]; // array containing YouTube Video IDs
    var movie = movies[Math.floor(Math.random() * movies.length)]; // pick one item from the array at random
    // now write out the tags for the movie
    document.write('<iframe width="420" height="315" src="http://www.youtube.com/embed/' + movie + '" frameborder="0" allowfullscreen></iframe>');
    </script>
    What you're randomly picking are the YouTube Video IDs found in the URL of the YouTube videos. For example, if your youtube link looks something like this:

    Code:
    http://www.youtube.com/watch?v=G4Z5G78viGc
    then you add the ID at the end, to your array in the javascript code:

    Code:
    http://www.youtube.com/watch?v=G4Z5G78viGc
    in more complicated youtube links, like this:

    Code:
    http://www.youtube.com/watch?v=Wv8u5bY8Now&feature=BFa&list=LLNMDr9BuwR2hPQqEx4NuxOg
    the ID is the code after ?v= and before the first occurring &, like this:

    Code:
    http://www.youtube.com/watch?v=Wv8u5bY8Now&feature=BFa&list=LLNMDr9BuwR2hPQqEx4NuxOg
    Code:
    http://www.youtube.com/watch?v=Wv8u5bY8Now&feature=BFa&list=LLNMDr9BuwR2hPQqEx4NuxOg
    Wv8u5bY8Now is then the ID you want to add in your array of YouTube video IDs.

    If you're interested in using PHP instead, then you'll need to have a server with PHP installed. Assuming you already have one, type this code instead of the javascript one:

    Code:
    <!-- in the body of the page where you want the movie to appear -->
    <?php
    $movies = array("-UNFQ1o5brM", "xI3y5dJlKEU", "Xgoe8jaiwX4"); // array containing YouTube Video IDs
    $movie = rand(0, count($movies)-1); // pick one item from the array at random
    // now write out the tags for the movie
    echo '<iframe width="420" height="315" src="http://www.youtube.com/embed/' . $movies[$movie] . '" frameborder="0" allowfullscreen></iframe>';
    ?>
    Hope this helps
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  6. #6
    Junior Member
    Join Date
    Aug 2012
    Posts
    2

    Thanks!

    Thanks, [Nig 13].

    At the moment, I've got around the problem by creating a YouTube playlist and embedding that, but it's not ideal (there isn't a "random shuffle" option on the YouTube playlist). So, I might revisit the
    problem and use your advice. Many many thanks!
    Paul

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center