PDA

View Full Version : Random Object Javascript?



Moglizorz
03-02-2007, 07:44 PM
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 :D.

- Daniel

jscheuer1
03-05-2007, 07:03 AM
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:


<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.

Moglizorz
03-05-2007, 05:37 PM
Sweet! TYTYTYTYTY!

Moglizorz
03-05-2007, 05:47 PM
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.

techno_race
03-11-2007, 03:05 AM
If you want random:


<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>

Moglizorz
03-11-2007, 09:39 PM
If you want random:


<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?

BLiZZaRD
03-13-2007, 12:39 AM
What are the exact swf names, and the movie dimensions?

There is an easier way, but I will need these to make it.

Moglizorz
03-13-2007, 07:57 PM
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 :p
You seem to have more knowledge than anyone lol :D

BLiZZaRD
03-13-2007, 09:42 PM
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 :)

Moglizorz
03-15-2007, 09:58 PM
Lol, don't be modest. You do rock :p

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 :D

BLiZZaRD
03-16-2007, 04:53 PM
The ONLY code in the movie is this:



i = random(2)+1;
loadMovie("swf"+i+".swf", "box_container");


This code assumes you name all your movies to load "swfX.swf" where X is a number.

If you want to add other movies (3, 4,583, 27, whatever) just number and name them in this way. Then change the number in the i = line to match the total number of movies.



i = random(4583)+1;
loadMovie("swf"+i+".swf", "box_container");


will choose randomly between your 4 thousand + movies. :D

You can do what ever you want with this movie, the code, what ever. I don't hold any copyrights to the code I just write it :D

Enjoy.

Moglizorz
03-18-2007, 12:19 AM
W00t. Thanks, I'll use this :D:D:D

You know, every time you have talked to me (the whole of 2 times! :p) you have broadened my knowledge. xD

BLiZZaRD
03-18-2007, 03:14 PM
That's what we are here for :D

If you really want to gain some knowledge test out my Riddle site ;) (shameless self promotion)

techno_race
03-18-2007, 06:23 PM
If you want random:
<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?

Yes. For example, if you want a green text string for one, you would change

html[1]="<button...>...</button>"
to

html[1]="<font color=\"green\">text string</font>"