Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Jan 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help me please :( Random image problems

    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

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    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
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    New Coder
    Join Date
    Jan 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    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()">

  • #5
    New Coder
    Join Date
    Jan 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you so much, i will try doing it again to night

  • #6
    New Coder
    Join Date
    Jan 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy

    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

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    You still have the variable name ry duplicated.

  • #8
    New Coder
    Join Date
    Jan 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    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?

  • #10
    New Coder
    Join Date
    Jan 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #11
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #12
    New Coder
    Join Date
    Jan 2005
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, it works already
    Thank you so much for both Willy and Phillip
    I really appreciate your help.

    Kotaro,
    Nice sleep tonight


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •