Javascript help meeees? (1 Viewer)

Johnnyc

Well-Known Member
Joined
May 5, 2004
Messages
3,402
Location
x
I need a script that will display a random swf. I have the code below for displaying a random image from a list, but as I say, I'd like to display a random .swf file instead. Can this blokes code below be modified to do such a thing?

I was pretty pissed off to find out I couldn't just change the reference to .jpg to .swf and discover that Bob was indeed my uncle.




<!--
// ==============================================
// Copyright 2003 by jsCode.com
// Source: jsCode.com
// Author: etLux
// Free for all; but please leave in the header.
// ==============================================

// Set up the image files to be used.
var theImages = new Array() // do not change this
// To add more image files, continue with the
// pattern below, adding to the array. Rememeber
// to increment the theImages[x] index!

theImages[0] = 'random1.jpg'
theImages[1] = 'random2.jpg

// ======================================
// do not change anything below this line
// ======================================

var j = 0
var p = theImages.length;

var preBuffer = new Array()
for (i = 0; i < p; i++){
preBuffer = new Image()
preBuffer.src = theImages
}

var whichImage = Math.round(Math.random()*(p-1));
function showImage(){
document.write('<img src="'+theImages[whichImage]+'">');
}

//-->
</script>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a.indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a;}}
}

function MM_swapImgRestore() { //v3.0
var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a)&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers.document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
 
yeah because flash movies need to be embedded using the <object> tag instead of just placed in an <img> tag.

in fact, I think the <object> tag is deprecated anyway, and thanks to microsoft being complete pricks, if you use it you' get an activex warning in IE and the movie won't load until you manually click on it. every time. pain in the hole.

the recommended way around this is to use swfobject

in any case I'd probaby pick a different route, which would be to create and embed a container flash movie, and use actionscript inside that to load your random .swf, using MovieClipLoader or loadMovie.
 
here's an example of code you could use in your container movie:

PHP:
/**
 * create a random swf filename 
 * between 1 and 10; e.g: "4.swf"
 */

var randomMovie:String = Math.ceil(Math.random() * 10) + ".swf";

/**
 * create a container movie clip
 * for the random swf to be loaded
 */

_root.createEmptyMovieClip("container_mc", _root.getNextHighestDepth());

/**
 * create an instance of MovieClipLoader object
 * and load random clip using the loadClip() method
 */

var loader:MovieClipLoader = new MovieClipLoader();
loader.loadClip(randomMovie, container_mc);

this way, you can just hard code your "container" .swf movie in html, negating the use of javascript to select and embed a random movie! as I was saying swfobject is probably the best way to embed flash movies these days and it also validates as good html, as well as avoiding the dreaded microsoft activex plugin warning. hope this helps!
 
yes. I suppose you could. but if it ain' broke... haven't really used AS3 yet. I'm still stuck in AS2 lala land until the next time I get a big flash project. I have a copy of this on my shelf but I've been too busy to open it..

30.jpg
 
if it ain't broke???
everything about as2 IS broke :)
seriously as3/flex is excellent, none of the as2 "what the fuck", "why jesus why", "it was working a minute ago", "how come putting a home button on the screen caused the server to crash" mentalness...
 

Users who are viewing this thread

Activity
So far there's no one here
Old Thread: Hello . There have been no replies in this thread for 365 days.
Content in this thread may no longer be relevant.
Perhaps it would be better to start a new thread instead.

21 Day Calendar

Lau (Unplugged)
The Sugar Club
8 Leeson Street Lower, Saint Kevin's, Dublin 2, D02 ET97, Ireland

Support thumped.com

Support thumped.com and upgrade your account

Upgrade your account now to disable all ads...

Upgrade now

Latest threads

Latest Activity

Loading…
Back
Top