PDA

View Full Version : Loading a different image on pageload?...Javascript array? Flash movies too? Oh my!



suction cup
11-15-2010, 12:04 PM
Dudes/Dudesses

I know next to nothing about Javascript, or web design in general, really, so please don't be offended if this seems a stupid or redundant question (!)...

...I've got the script for an image array, to load a different image each time the page loads...

...is it possible to alter this so that it will also load .swf movies as well as .gifs?

I tried changing the usual array listing e.g:

imageArray[1] = "images/image1.gif";

to, for example:

imageArray[1] = "images/flash1.swf";

...but, um - that doesn't work. At all. :eek:

If anyone can help in any way, I would really appreciate it. This may be a retarded question, but as previously, my knowledge level here is very basic.

As a guess, I'd say that this has something to do with either, or both, of these factors:

A) The array being titled 'image'Array - maybe it only recognizes images, and not movies (and I believe an .swf is classed as such).

B) The link to the .swf (e.g. "images/image.swf") does not contain the usual - longer-winded - code that helps the .swf load up (as you would get in html; <embed src> etc...). Maybe the question is, how to get all those details in the Javascript array itself...?

Many Thanks. Honestly, any help here is so appreciated. It may also save the computer from being physically harmed too.

Full script of the array in question follows....

:)

var imageArray = new Array();

imageArray[0] = "images/image.gif";
imageArray[1] = "images/image1.gif";
imageArray[2] = "images/image2.gif";


function doIt()
{
var rand = Math.floor(Math.random()*3);
var imgPath = "<img src='"+imageArray[rand]+"' alt='Welcome' border='0' align='absmiddle' />";
document.getElementById("image").innerHTML = imgPath;
}

:)

lilpete
11-15-2010, 02:10 PM
Hi
No question is a stupid question if you dont know the answer.. :D

Any chance of a link to the page in question so I can see the whole thing?

Also you can see here:-


{
var rand = Math.floor(Math.random()*3);
var imgPath = "<img src='"+imageArray[rand]+"' alt='Welcome' border='0' align='absmiddle' />";
document.getElementById("image").innerHTML = imgPath;
}

This script is creating the html for an image tag when it runs, so the output when you change the imagearrays to imageArray[1] = "images/flash1.swf";
is something like this on the page:-
<img src="images/flash1.swf" alt="Welcome" border="0" align="absmiddle" />

and as you have mentioned flash needs a lot of other code to load up.. maybe the answer is to find a script with similar functionality designed for loading flash files..

Like I said shoot us a link to the page and we can all have a proper look then. :D

Best Regards,
Pete

suction cup
11-15-2010, 03:08 PM
Hi Pete,

Cheers. Suspect ridiculous question as it was posted on an alternative forum and received no replies from several hundred views. OK, it's a relevant question :)

The site isn't fully working (with all this code in), so I think it would confuse things to link to it.

Thanks for pointing out that there's actually html in there - am guessing that having speech marks (") means it stops being Javascript and becomes html?

So; hypothetically:

imageArray[0] = "images/image.gif";
imageArray[1] = "images/image1.gif";
imageArray[2] = "images/image2.swf";

then: imgArray[random]

if it generates- 0 or 1: imgPath = //to the gifs as before
if it generates- 2: imgPath = //to the swfs with different html

Does that make sense? It's not meant to be proper code, but is that a possibility based on what you said in your message? I don't know how you would write the Javascript to choose between these options based on the random number generated. An if/or function of some kind, perhaps.

Thanks.

Regards,
Howard

lilpete
11-15-2010, 03:43 PM
Good stuff, Ill come clean, im no Javascript 'scripter' but I can make sense of things..
:) Also im at work now, so plenty of looking over my shoulder going on.. lol

I found this:-it may help in the meantime..

http://www.dynamicdrive.com/forums/archive/index.php/t-13723.html

I will have another look when I get home tonight..

But yes, you are on the right lines.. a script that contains both sets of html, then the html that is used is selected from the imagearray itself.. :)

Maybe one of the wonderful coders on here could put that lot together...? *nudge nudge* ;)

Best Regs,
Pete

suction cup
11-15-2010, 05:21 PM
Thanks a lot; :)

...that link looks to really have the goods; just need to switch around some of the functions to be .gif relevant...

...I'll check it later when there's also more time - this looks like it'll work, but of course if anyone would like to also explore this interesting area too...:rolleyes:

Hey! This site is good honest family fun, no? Surely no-one will object to THIS! :eek:

suction cup
11-16-2010, 01:23 PM
I found this:-it may help in the meantime..

http://www.dynamicdrive.com/forums/archive/index.php/t-13723.html



Hello!

Am posting in the hope that someone can relate to this: Loading a mix of random flash movies and .gif animations!

[Pete, your assistance is still actively encouraged!!! ;);)]!

Blending the script from the above link with the one I had which would load us up a .gif, we have the script below.

I don't currently understand this enough to say why it doesn't work!

Heck, I'm not even sure how the Javascript is supposed to 'link' to something in the body of the html page...in the original Javascript for the .gif array, the html in the body had an id (="image"), which seemed to 'apply' the Javascript to that particular location etc. How would you link to the flash movies, would you use the getelementbyId jazz again? I kept the original function, "doIt()", which called up the Javascript in the orignial array, with the gifs.

Phew.

I'm completely out of my depth with this btw :eek: Maybe I should do my homework before asking you guys, but this seems to be the kind of thing which is best learned with examples..............?!

Code follows (deep breath...) ;)

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
yeah
</title>
<meta name="GENERATOR" content="Arachnophilia 5.4"/>
<meta name="FORMATTER" content="Arachnophilia 5.4"/>

<script language="JavaScript" type="text/javascript">



<!--
// -----------------------------------------------------------------------------
// Globals
// Major version of Flash required
var requiredMajorVersion = 6;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Minor version of Flash required
var requiredRevision = 0;
// -----------------------------------------------------------------------------
// -->
</script>



<!-- FLASH OBJECT -->
<script language="JavaScript" type="text/javascript">
<!--

function doIt()
{
// Version check based upon the values entered above in "Globals"
var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);

// Check to see if the version meets the requirements for playback
if (hasReqestedVersion)

// if we've detected an acceptable version
// embed the Flash Content SWF when all tests are passed
// Generate a Random Number
var randomnumber = Math.round(Math.random()*3);

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

{movie1();}

else if (randomnumber == 2)

{movie2();}

else {animation1();
}

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

function movie1(){
AC_FL_RunContent(
"src", "/images/withLeaves",
"play", "true",
"width", "980",
"height", "323",
"align", "middle",
"wmode", "window",
"id", "/images/withLeaves",
"quality", "high",
"bgcolor", "#FFFFFF",
"name", "/images/withLeaves",
"allowScriptAccess","sameDomain",
"type", "application/x-shockwave-flash",
'codebase', 'http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab',
"pluginspage", "http://www.macromedia.com/go/getflashplayer"
)
}

function movie2(){
AC_FL_RunContent(
"src", "/images/movie2",
"play", "true",
"width", "980",
"height", "323",
"align", "middle",
"wmode", "window",
"id", "/images/movie2",
"quality", "high",
"bgcolor", "#FFFFFF",
"name", "/images/movie2",
"allowScriptAccess","sameDomain",
"type", "application/x-shockwave-flash",
'codebase', 'http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab',
"pluginspage", "http://www.macromedia.com/go/getflashplayer"
)
}

function animation1(){

var imgPath = "<img src='"images/animation1.gif"' alt='Welcome' border='0' align='absmiddle'/>";

document.getElementById("image").innerHTML = imgPath;
}


} else { // flash is too old or we can't detect the plugin
var alternateContent = '<a href="http://www.interacoustics-us.com/Products/ABR_EvokedPotentials/InteracousticsASSR.asp"><img src="/images/ASSR2.gif" width="700" height="276" border="0"></a>';
document.write(alternateContent); // insert non-flash content
}
// -->
</script>



</head>

<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">

<div id="image"><script type="text/javascript">
doIt();
</script>
</div>



</body></html>


Thank-you :)

suction cup
11-16-2010, 01:37 PM
I found this:-it may help in the meantime..

http://www.dynamicdrive.com/forums/archive/index.php/t-13723.html


STOP PRESS!

:D
Just after the previous I realised it's easy to convert gifs to flash - so that really simplifies things....! :):rolleyes:

I'm still not sure how to use the code from the above link to actually embed the movie in the page...but will be trying some things out...

Thanks guys