...

View Full Version : Help me please :( Random image problems



kotaro
01-20-2005, 05:00 PM
Hello everyone,

It is possible to add javascript random image code two times in one page?
I have two sets of my photo ( member photo and music photo), and i really want them to random in the first page at the same time.
For the first set, it worked properly. However, when i added the same code in the same page, the second set did not work.
do i have to change anything when i want to add the same code in one page? Any suggestion for me please.

Looking forward to hearing from you all soon.

Regards,

Kotaro :(

A1ien51
01-20-2005, 05:47 PM
Show us the code/page you are using.

The reason why it is not working is probably becuase of variables clashing or multiple onload handlers.

Eric

kotaro
01-21-2005, 01:33 AM
here is the code
------------------------------------------
in my web, the body tag are as follow

<body topmargin="0" leftmargin="0">

------------------------------------------
--------------------------------------------------------------------------
My first set of java script

<script language="JavaScript">
<!--

/*
Random Image Link Script- By JavaScript Kit(http://www.javascriptkit.com)
Over 200+ free JavaScripts here!
Updated: 00/04/25
*/

function random_imglink(){
var myimages=new Array()
//specify random images below. You can have as many as you wish
myimages[1]="http://www.geocities.com/vainumlao/P-Feb-01.gif"
myimages[2]="http://www.geocities.com/vainumlao/P-Feb-02.gif"

//specify corresponding links below
var imagelinks=new Array()
imagelinks[1]="http://groups.msn.com/laopys/gurlygurl.msnw"
imagelinks[2]="http://groups.msn.com/laopys/sisava.msnw?albumlist=2"

var ry=Math.floor(Math.random()*myimages.length)
if (ry==0)
ry=1
document.write('<a href='+'"'+imagelinks[ry]+'"'+'><img src="'+myimages[ry]+'" border=0></a>')
}
random_imglink()
//-->
</script>
------------------------------------------------------------------------

My second set of java script

<script language="JavaScript">
<!--

/*
Random Image Link Script- By JavaScript Kit(http://www.javascriptkit.com)
Over 200+ free JavaScripts here!
Updated: 00/04/25
*/

function random_imglink(){
var myimages=new Array()
//specify random images below. You can have as many as you wish
myimages[1]="http://www.geocities.com/vainumlao/book.gif"
myimages[2]="http://www.geocities.com/vainumlao/book2.gif"

//specify corresponding links below
var imagelinks=new Array()
imagelinks[1]="http://www.yahoo.com"
imagelinks[2]="http://www.hotmail.com"

var ry=Math.floor(Math.random()*myimages.length)
if (ry==0)
ry=1
document.write('<a href='+'"'+imagelinks[ry]+'"'+'><img src="'+myimages[ry]+'" border=0></a>')
}
random_imglink()
//-->
</script>
------------------------------------------------------------------------

in what place do i need to change the script?

Regards,

Kotaro

Philip M
01-21-2005, 08:30 AM
You have simply duplicated the script so all your functions and variables have the same name.

You need to alter them to (e.g.) random_imglinkA and random_imglinkB,
myimagesA and myimagesB, and so forth.

Note that you may not have multiple onload handlers either.

You must have something like

<BODY onload="random_imglinkA(); random_imglinkB()">

kotaro
01-21-2005, 10:24 AM
Thank you so much, i will try doing it again to night :)

kotaro
01-21-2005, 04:55 PM
I followed all your suggestion, but i found that it still not work. Besides, when i v iew the page, other information disapear.
Could you please help me check out the code again.
here what i did

----------------------------------
<body onload="random_imglinkA(); random_imglinkB()" topmargin="0" leftmargin="0">


<script language="JavaScript">
<!--

/*
Random Image Link Script- By JavaScript Kit(http://www.javascriptkit.com)
Over 200+ free JavaScripts here!
Updated: 00/04/25
*/

function random_imglinkA(){
var myimagesA=new Array()
//specify random images below. You can have as many as you wish
myimagesA[1]="http://www.geocities.com/vainumlao/P-Feb-01.gif"
myimagesA[2]="http://www.geocities.com/vainumlao/P-Feb-02.gif"

//specify corresponding links below
var imagelinksA=new Array()
imagelinksA[1]="http://groups.msn.com/laopys/gurlygurl.msnw"
imagelinksA[2]="http://groups.msn.com/laopys/sisava.msnw?albumlist=2"

var ry=Math.floor(Math.random()*myimagesA.length)
if (ry==0)
ry=1
document.write('<a href='+'"'+imagelinksA[ry]+'"'+'><img src="'+myimagesA[ry]+'" border=0></a>')
}
random_imglinkA()
//-->
</script>
------------------------------------------------------------------------

My second set of java script

<script language="JavaScript">
<!--

/*
Random Image Link Script- By JavaScript Kit(http://www.javascriptkit.com)
Over 200+ free JavaScripts here!
Updated: 00/04/25
*/

function random_imglinkB(){
var myimagesB=new Array()
//specify random images below. You can have as many as you wish
myimagesB[1]="http://www.geocities.com/vainumlao/book.gif"
myimagesB[2]="http://www.geocities.com/vainumlao/book2.gif"

//specify corresponding links below
var imagelinksB=new Array()
imagelinksB[1]="http://www.yahoo.com"
imagelinksB[2]="http://www.hotmail.com"

var ry=Math.floor(Math.random()*myimagesB.length)
if (ry==0)
ry=1
document.write('<a href='+'"'+imagelinksB[ry]+'"'+'><img src="'+myimagesB[ry]+'" border=0></a>')
}
random_imglinkB()
//-->
</script>
------------------------------------------------------------------------


Please correct it for me when you find a mistake on it.

Regards,

Kotaro

Philip M
01-21-2005, 07:37 PM
You still have the variable name ry duplicated.

kotaro
01-22-2005, 03:20 AM
Hi here is what i have changed the code, but seem that it still not work
can you check it for me again

--------------------------------------------------------------------
<body onload="random_imglinkA(); random_imglinkB()" topmargin="0" leftmargin="0">


<script language="JavaScript">
<!--

/*
Random Image Link Script- By JavaScript Kit(http://www.javascriptkit.com)
Over 200+ free JavaScripts here!
Updated: 00/04/25
*/

function random_imglinkA(){
var myimagesA=new Array()
//specify random images below. You can have as many as you wish
myimagesA[1]="http://www.geocities.com/vainumlao/P-Feb-01.gif"
myimagesA[2]="http://www.geocities.com/vainumlao/P-Feb-02.gif"

//specify corresponding links below
var imagelinksA=new Array()
imagelinksA[1]="http://groups.msn.com/laopys/gurlygurl.msnw"
imagelinksA[2]="http://groups.msn.com/laopys/sisava.msnw?albumlist=2"

var ryA=Math.floor(Math.random()*myimagesA.length)
if (ryA==0)
ryA=1
document.write('<a href='+'"'+imagelinksA[ryA]+'"'+'><img src="'+myimagesA[ryA]+'" border=0></a>')
}
random_imglinkA()
//-->
</script>
------------------------------------------------------------------------

My second set of java script

<script language="JavaScript">
<!--

/*
Random Image Link Script- By JavaScript Kit(http://www.javascriptkit.com)
Over 200+ free JavaScripts here!
Updated: 00/04/25
*/

function random_imglinkB(){
var myimagesB=new Array()
//specify random images below. You can have as many as you wish
myimagesB[1]="http://www.geocities.com/vainumlao/book.gif"
myimagesB[2]="http://www.geocities.com/vainumlao/book2.gif"

//specify corresponding links below
var imagelinksB=new Array()
imagelinksB[1]="http://www.yahoo.com"
imagelinksB[2]="http://www.hotmail.com"

var ryB=Math.floor(Math.random()*myimagesB.length)
if (ryB==0)
ryB=1
document.write('<a href='+'"'+imagelinksB[ryB]+'"'+'><img src="'+myimagesB[ryB]+'" border=0></a>')
}
random_imglinkB()
//-->
</script>
------------------------------------------------------------------------

please help me correct the code above.

Regards,

Kotaro :(

Philip M
01-22-2005, 09:47 AM
You have called the scripts by a body onload event handler so these two
statements are not required.

random_imglinkA()

random_imglinkB()

Could you say how it is not working? What error messages do you get, or what does not produce the desired result?

kotaro
01-22-2005, 01:16 PM
Hi Philip M,
Now it works already, but only photos.
I mean that all other information like text, table and other photos dissapear, and i can see only a two set of random photos in the top most of the page.

I put the javascript in the table? does it work in the table?

I am sorry to disturb you again and again, but i really want to see it work.

Please help me again.

Kotaro

Willy Duitt
01-22-2005, 01:30 PM
You can not call any script that uses document.write body.onload because it will overwrite the document... That is why you only see the two random links/images...

Remove the onload event function call, return the function calls at the bottom of the scripts which you were just advised to remove and place those scripts within the body of the documant where you would like the random links/images to appear...

If you continue to have problems, it would be better to post a link to the document so someone can edit the actual page codes and get this working for you...

.....Willy

kotaro
01-22-2005, 02:28 PM
Yes, it works already
Thank you so much for both Willy and Phillip
I really appreciate your help. :)

Kotaro,
Nice sleep tonight :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum