Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with a random .swf script 1

Status
Not open for further replies.

tmstark

Technical User
Nov 11, 2006
5
US
Hello. I need help with a random .swf script. The script I have works fine but I want to modify it so that the same .swf banner will not load twice in a row. Here is my script as it is now:

<script language="JavaScript">

// Generate a Random Number
var randomnumber = Math.round(Math.random()*5);

// Select a movie and execute the corresponding function
if (randomnumber == 1)

{movie1();}

else if (randomnumber == 2)

{movie2();}

else if (randomnumber == 3)

{movie3();}

else if (randomnumber == 4)

{movie4();}

else {movie5();}


//Functions to write out the correct flash movie resource.

function movie1(){
document.write("<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"

codebase=\"
width=\"778\" height=\"146\"><param name=movie value=\"movie1.swf\"><param name=quality

value=high><embed src=\"movie1.swf\" quality=high

pluginspage=\"
ash\" type=\"application/x-shockwave-flash\" width=\"778\" height=\"146\"></embed></object>")
}

function movie2(){
document.write("<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"

codebase=\"
width=\"778\" height=\"146\"><param name=movie value=\"movie2.swf\"><param name=quality

value=high><embed src=\"movie2.swf\" quality=high

pluginspage=\"
ash\" type=\"application/x-shockwave-flash\" width=\"778\" height=\"146\"></embed></object>")
}

function movie3(){
document.write("<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"

codebase=\"
width=\"778\" height=\"146\"><param name=movie value=\"movie3.swf\"><param name=quality

value=high><embed src=\"movie3.swf\" quality=high

pluginspage=\"
ash\" type=\"application/x-shockwave-flash\" width=\"778\" height=\"146\"></embed></object>")
}

function movie4(){
document.write("<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"

codebase=\"
width=\"778\" height=\"146\"><param name=movie value=\"movie4.swf\"><param name=quality

value=high><embed src=\"movie4.swf\" quality=high

pluginspage=\"
ash\" type=\"application/x-shockwave-flash\" width=\"778\" height=\"146\"></embed></object>")
}

function movie5(){
document.write("<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"

codebase=\"
width=\"778\" height=\"146\"><param name=movie value=\"movie5.swf\"><param name=quality

value=high><embed src=\"movie5.swf\" quality=high

pluginspage=\"
ash\" type=\"application/x-shockwave-flash\" width=\"778\" height=\"146\"></embed></object>")
}

</script>

Can somebody help me? I'm a noob, so talk down to me.
 
I'd replace that whole script with the following:

Code:
<script type="text/javascript">

	var lastMovieNum = -1;

	// Pick movie number
	var randomNum = Math.round(Math.random() * 5);

	// Ensure it's not the same as last time
	while (randomNum == lastMovieNum) {
		var randomNum = Math.round(Math.random() * 5);
	}
	lastMovieNum = randomNum;
	
	var movieFilename = 'movie' + randomNum + '.swf';
	movie(movieFilename);
	
	function movie(movieFilename){
		document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="[URL unfurl="true"]http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0"[/URL] width="778" height="146"><param name="movie" value="' + movieFilename + '"><param name="quality" value="high"><embed src="' + movieFilename + '" quality="high" pluginspage="[URL unfurl="true"]http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"[/URL] type="application/x-shockwave-flash" width="778" height="146"></embed></object>');
	}

</script>

I'm a noob, so talk down to me.

It's no fun doing that when you've asked for it ;-)

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hey. Sorry about the late response. I thank you for your help. While the code you gave me is certainly a lot cleaner, it didn't solve the problem I had, namely, banner1 (or 2-5) can still occur two or more times in a row. What would be the code to tell it not to do that?
 
it didn't solve the problem

Really? The "while" statement in the code should have seen to that. Are you sure you're seeing the same banner twice in a row?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Yeah. I'm positive. I just don't know what to make of it. Check out
The code you gave me is used there, unaltered.

There are five banners, each with a different set of pictures.

Every once in a while, one banner will appear twice or more in a row.

Plus, every so often, nothing will load in the banner space, as though it's calling for something that isn't there.

I looked over your code multiple times, but couldn't find the cause of that.
 
Thanks BillyRayPreachersSon for the nice script!
I was looking for "random swf" script aswell and came across this board. ^^

tmstark said:
Plus, every so often, nothing will load in the banner space, as though it's calling for something that isn't there.
I looked over your code multiple times, but couldn't find the cause of that.
Hallo tmstark,
ive noticed those blanks too.
changing:
var lastMovieNum = -1;
to
var lastMovieNum = 0;
will fix it. ^^

Also like youve said, the randomiser that should not load the same swf twice in a row, doesnt work....
Im no programmer, but can such JS really still remember the number of the last shown swf file, after you hit refresh in your browser, or reload the page by clicking on a link?? I belive it cant and if its like that, its not possible to create such script(but correct me if iam wrong).

Ive modified your script just a lil' bit, BillyRayPreachersSon (i hope you dont mind ^^),
which just shows random image from 1-4.

Code:
<script type="text/javascript">
    var lastMovieNum = 0;
    var randomNum = 0

    // Randomize .swf
    while (randomNum == lastMovieNum) {
    var randomNum = Math.round(Math.random() * 4);
    }
    
    // Just standard html Flash Code for all .swf objects
    var objnfo1 = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="[URL unfurl="true"]http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0"><param[/URL] name="movie" value="' + movieFilename + '"><param name="quality" value="high">';
    var objnfo2 = 'quality="high" pluginspage="[URL unfurl="true"]http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"[/URL] type="application/x-shockwave-flash"></embed></object>';
    
    // SWF's Info (# change directory and filename prefix bellow #)
    var movieFilename = 'swf_ani/ani_' + randomNum + '.swf';
    movie(movieFilename);

    // Code to be inserted into html (# change dimensions of the .swf file bellow #)    
    function movie(movieFilename){
        document.write('  '+ objnfo1 +'<embed src="'+ movieFilename +'" width="400" height="320" '+ objnfo2 +' ');
    }
</script>


Thats pretty much what i needed, BillyRayPreachersSon, thanks again! (but if i was wrong about the randomizer thing and there is a way not to show the same swf file twice in a row please let us know)
 
Hi BillyRay et al

I used your script to embed 4 random flash movies - I like it a lot, works for me in safari etc, but it doesn't seem to do anything in IE 6 or 7 - nothing loads where the swfs should be...

Any ideas???

Thanks and a Happy New Year

CLUCAS
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top