Forum Moderators: open

Message Too Old, No Replies

Using javascript to generate random javascript

Using javascript to generate random javascript

         

mattboy slim

11:08 pm on Nov 28, 2006 (gmt 0)

10+ Year Member



I know, the topic is confusing, but I'm in quite the pickle. I'm using SWFObject (formerly FlashObject (http://blog.deconcept.com/swfobject/)) to embed my Flash because of the Eolas/Microsoft debacle. Anyways....

I want to have my page display a random Flash movie (.swf) every time the page loads (I have 4 total movies). Had I not been using the SWFObject, really any random javascript code would work, but I'm having some difficulty using javascript within javascript. I'm not going to show you the code that I had, since it doesn't work anyways, and I don't need to muddy the situation. Below I have pasted the code I am using to generate my Flash:


<script type="text/javascript" src="_scripts/flashobject.js"> </script>

<div id="flashcontent" style="width:100%;height:348px"></div>

<script type="text/javascript">
var fo = new FlashObject("flash/header.swf", "animationName", "100%", "348", "8", "");
fo.addParam("allowScriptAccess", "sameDomain");
fo.addParam("quality", "high");
fo.addParam("scale", "noscale");
fo.addParam("loop", "false");
fo.write("flashcontent");
</script>


So what I need, essentially, is to randomly choose between header.swf, header2.swf, header3.swf, and header4.swf. Of course, I most likely will add more in the future, but we'll stick with 4 for now.

Thanks in advance for any help or advice you can give,
Matt

jonrichd

12:07 am on Nov 29, 2006 (gmt 0)

10+ Year Member



Hello Matt, Welcome to Webmasterworld.

If you can control the naming of your flash files (and I assume you can) name them sequentially (flash1, flash2, flash3, etc). Then use Javascript to create a random number between 1 and 4 (or however many flash files you have), and assign it to a variable.

Then use something like:

var fo = new FlashObject("flash/header' + randomnumbervariable + '.swf", "animationName", "100%", "348", "8", "");

I may not have your exact syntax right, but I use somthing similar to display a random image on one of my sites.

mattboy slim

2:09 pm on Nov 29, 2006 (gmt 0)

10+ Year Member



Jon, thanks for the reply.

Here is my situation: I know about as much about javascript as I know about the formulas of the string theorgy in quantum physics...which is nothing. I'm more of a, how do you say..."copy and paste" guy when it comes to javascript.

jonrichd

2:34 pm on Nov 29, 2006 (gmt 0)

10+ Year Member



Matt,

Between your knowledge of Javascript and my knowledge of Flash, we both know nothing. However, I'll post a little more of your code, modified with the Javascript I use. It may work, or it may not:


<script type="text/javascript">
' put in the random number function
rnd.today=new Date();
rnd.seed=rnd.today.getTime();
maximages=4; ' set this to the total number of flash images you have
function rnd() {
rnd.seed = (rnd.seed*9301+49297) % 233280;
return rnd.seed/(233280.0);
};
function rand(number) {
return Math.ceil(rnd()*number);
};
' the original code, with the next line modified for the max images
var fo = new FlashObject("flash/header' + rand(maximages) + '.swf", "animationName", "100%", "348", "8", "");
fo.addParam("allowScriptAccess", "sameDomain");
fo.addParam("quality", "high");
fo.addParam("scale", "noscale");
fo.addParam("loop", "false");
fo.write("flashcontent");
</script>

If you number your flash images as outlined in the previous message, this may work, although I don't know about the animationName variable - that's where your Flash knowledge comes in.