Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Random Object Javascript?

  1. #1
    Join Date
    Mar 2007
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Random Object Javascript?

    Just ask example, I have 2 .swf files.
    • first.swf
    • second.swf

    Does anyone know of a script that picks either one of those 2 to go onto a spot on my website? As an example, if it was say... A random picture generator, and 'first.swf' and 'second.swf' were the images, I would want it so whenever you refresh the page, it shows a different picture. So when you go on the site it might show 'first.swf' but when you go to it again it will show 'second.swf' in the same spot. If ya know what I mean. Does anyone know how to do this? That would be great .

    - Daniel

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Do you want random or alternating? Random is easier but, with random, there can easily be a string of repeats, especially if the pool of choices is limited.

    I'm no expert on Flash but, its Action Script is very much like javascript so, there may be a way to get a single swf to branch randomly each time it is loaded and in essence appear differently on different loads. Javascript may also be used to write out the object tag, selecting a random swf from an array:

    Code:
    <script type="text/javascript">
    (function(){
    var theSwf=["swf1.swf", "swf2.swf", "swf3.swf", "swf4.swf"];
    theSwf.sort(function() {return 0.5 - Math.random();})
    
    document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"\n\
               codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"\n\
               width="362" height="287">\n\
    			<param name="movie" value="'+theSwf[0]+'"> \n\
    			<param name="quality" value="high">\n\
    			<param name="wmode" value="transparent">\n\
    			<param name="menu" value="false">\n\
    			<!--[if !IE]> <-->\n\
    			<object data="'+theSwf[0]+'"\n\
    					width="362" height="287" type="application/x-shockwave-flash">\n\
    			 <param name="quality" value="high">\n\
    			 <param name="wmode" value="transparent">\n\
    			 <param name="menu" value="false">\n\
    			 <param name="pluginurl" value="http://www.macromedia.com/go/getflashplayer">\n\
    			 FAIL (the browser should render some flash content, not this).\n\
    			</object>\n\
    			<!--> <![endif]-->\n\
    		   </object>\n');
    		   })();
    </script>
    If you make it an external script, you get the added benefit of avoiding the 'click to activate' feature.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2007
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Sweet! TYTYTYTYTY!

  4. #4
    Join Date
    Mar 2007
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Also, random is fine. And I can't do it with action script, or I would. Because I don't have one the files .fla's. Only have that for 1 file, and just the .swf for the other.

  5. #5
    Join Date
    Feb 2007
    Location
    🌎
    Posts
    528
    Thanks
    10
    Thanked 10 Times in 10 Posts
    Blog Entries
    2

    Default

    If you want random:

    Code:
    <script type="text/javascript">
    
    var html=new Array()
    html[0]='<button onClick=\"window.location=\'http://www.dynamicdrive.com\'><font style=\"text-decoration: blink\">Random Link</font></button>';
    html[1]='<button onClick=\"window.location=\'http://www.javascriptkit.com\'><font style=\"text-decoration: blink\">Random Link</font></button>';
    html[2]='<button onClick=\"window.location=\'http://www.hotscripts.com/JavaScript/Scripts_and_Programs\'><font style=\"text-decoration: blink\">Random Link</font></button>';
    html[3]='<button onClick=\"window.location=\'http://www.macromedia.com\'><font style=\"text-decoration: blink\">Random Link</font></button>';
    html[4]='<button onClick=\"window.location=\'http://channels.netscape.com/ns/browsers/default.jsp\'><font style=\"text-decoration: blink\">Random Link</font></button>';
    html[5]='<button onClick=\"window.location=\'http://www.microsoft.com/windows/ie/default.htm\'><font style=\"text-decoration: blink\">Random Link</font></button>';
    html[6]='<button onClick=\"window.location=\'http://www.mozilla.com\'><font style=\"text-decoration: blink\">Random Link</font></button>';
    html[7]='<button onClick=\"window.location=\'http://www.cgiscripts.net\'><font style=\"text-decoration: blink\">Random Link</font></button>';
    html[8]='<button onClick=\"window.location=\'http://www.hotscripts.com\'><font style=\"text-decoration: blink\">Random Link</font></button>';
    html[9]='<button onClick=\"window.location=\'http://www.snapfiles.com\'><font style=\"text-decoration: blink\">Random Link</font></button>';
    
    var dothemath = Math.floor(Math.random()*html.length);
    document.write(html[dothemath]);
    </script>

  6. #6
    Join Date
    Mar 2007
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by techno_race View Post
    If you want random:

    Code:
    <script type="text/javascript">
    
    var html=new Array()
    html[0]='<button onClick=\"window.location=\'http://www.dynamicdrive.com\'><font style=\"text-decoration: blink\">Random Link</font></button>';
    html[1]='<button onClick=\"window.location=\'http://www.javascriptkit.com\'><font style=\"text-decoration: blink\">Random Link</font></button>';
    html[2]='<button onClick=\"window.location=\'http://www.hotscripts.com/JavaScript/Scripts_and_Programs\'><font style=\"text-decoration: blink\">Random Link</font></button>';
    html[3]='<button onClick=\"window.location=\'http://www.macromedia.com\'><font style=\"text-decoration: blink\">Random Link</font></button>';
    html[4]='<button onClick=\"window.location=\'http://channels.netscape.com/ns/browsers/default.jsp\'><font style=\"text-decoration: blink\">Random Link</font></button>';
    html[5]='<button onClick=\"window.location=\'http://www.microsoft.com/windows/ie/default.htm\'><font style=\"text-decoration: blink\">Random Link</font></button>';
    html[6]='<button onClick=\"window.location=\'http://www.mozilla.com\'><font style=\"text-decoration: blink\">Random Link</font></button>';
    html[7]='<button onClick=\"window.location=\'http://www.cgiscripts.net\'><font style=\"text-decoration: blink\">Random Link</font></button>';
    html[8]='<button onClick=\"window.location=\'http://www.hotscripts.com\'><font style=\"text-decoration: blink\">Random Link</font></button>';
    html[9]='<button onClick=\"window.location=\'http://www.snapfiles.com\'><font style=\"text-decoration: blink\">Random Link</font></button>';
    
    var dothemath = Math.floor(Math.random()*html.length);
    document.write(html[dothemath]);
    </script>
    So for that, I would just replace from <button...to...button> on each line, and add or remove lines as I please for different random HTML strings?

  7. #7
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    What are the exact swf names, and the movie dimensions?

    There is an easier way, but I will need these to make it.
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  8. #8
    Join Date
    Mar 2007
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    This page will be in the same directory as the files, which I will probably just name "swf1.swf" and "swf2.swf" for ease. The dimensions are 854px (width) by 196px (height).
    I can't beleive YOU replied. Your like the 1337est guy on here
    You seem to have more knowledge than anyone lol

  9. #9
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    Heh. Flattery will get you no where! I am by FAR NOT the most knowledgeable guy around here.

    Granted I know more about Flash than most here, but that's about it. I am not even that good at Flash, I just try a lot of things with Flash because I really enjoy Flash. They say to be good, really good, in Flash you need practice. They say about 100,000 lines of hand written code should get you a good start.

    I have hand written about 80,000. Although I have copied, pasted and tweaked about 3 million lines

    Close but not there yet

    Anyway, yes, make sure your swf's are in the same directory and this movie will do everything you need. Just be warned that randomizing only 2 movies will result in a butt ton of repeated ones. The more you add the better the randomizing will be
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  10. #10
    Join Date
    Mar 2007
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Lol, don't be modest. You do rock

    And nice flash randomizer thing! Is it ok if I use this in future stuff and just change the resolution etc? Can you please write the action script here for what I would change to get it to 3 files? Once I see that I should beable to figure out more than 3. Thanks alot BTW

Bookmarks

Posting Permissions

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