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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New to the CF scene
    Join Date
    Jun 2004
    Location
    UK
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    2 random image scripts on the same page - problems?

    hi

    I would like to use 2 instances of your funky random image on the same page to include one image at the top and funnily enough, one at the bottom.....
    I also want to include them onto my page/s with an external .js file..ie rand1.js and rand2.js, both external files having different links/image info.

    So...my question is, can anyone give me advice as to whether there will be any conflicts such as duplicate random images/links etc?

    Thanks for your time


    Josie

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yep, there's going to be some conflicts all right.

    Go back to where you got the script and follow the link right there on that page which leads you to the tutorial on how to handle the problem.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #3
    New to the CF scene
    Join Date
    Jun 2004
    Location
    UK
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for replying

    I wasn't too sure as to which links you were talking about...I've already read this and this and to be honest, I don't understand how I can implement the suggestions in those pages to the problem I have.

    My knowledge of javascript is very limited, just in case you hadn't noticed , in your opinion is what I want to do going to be a major pain in the arse to get to work right or is it pretty simple?

    ta

  • #4
    Senior Coder
    Join Date
    Jul 2003
    Location
    My pimped-out igloo in Canadia
    Posts
    1,966
    Thanks
    36
    Thanked 0 Times in 0 Posts
    generally when trying to get a script to work in twice on the same page, unless it's designed to handle multiple instances of itself, all you need to do is rename all the variable names & such... after that to the browser it's a completly new script.

    i just added a "2" to the end of every variable name... it should work for you this way

    Code:
    <script language="JavaScript">
    <!--
    
    /*
    Random Content Script- By JavaScript Kit(http://www.javascriptkit.com) 
    Over 200+ free JavaScripts here!
    */
    
    function random_content2(){
    var mycontent2=new Array()
    //specify random content below.
    mycontent2[1]='<b>Random content 1</b>'
    mycontent2[2]='<b>Random content 2</b>'
    mycontent2[3]='<b>Random content 3</b>'
    mycontent2[4]='<b>Random content 4</b>'
    mycontent2[5]='<b>Random content 5</b>'
    
    
    var ry=Math.floor(Math.random()*mycontent2.length)
    if (ry==0)
    ry=1
    document.write(mycontent2[ry])
    }
    random_content2()
    //-->
    </script>
    Code:
    <script language="JavaScript">
    <!--
    
    /*
    Random Content Script- By JavaScript Kit(http://www.javascriptkit.com) 
    Over 200+ free JavaScripts here!
    */
    
    function random_content(){
    var mycontent=new Array()
    //specify random content below.
    mycontent[1]='<b>Random content a</b>'
    mycontent[2]='<b>Random content s</b>'
    mycontent[3]='<b>Random content f</b>'
    mycontent[4]='<b>Random content g</b>'
    mycontent[5]='<b>Random content h</b>'
    
    
    var ry=Math.floor(Math.random()*mycontent.length)
    if (ry==0)
    ry=1
    document.write(mycontent[ry])
    }
    random_content()
    //-->
    </script>
    it works, toss both those babies onto one html page and away you go
    Last edited by canadianjameson; 06-16-2004 at 01:35 AM.
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes :)

  • #5
    New to the CF scene
    Join Date
    Jun 2004
    Location
    UK
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
    now that is what I call a simple and tres funky solution....hoorah!

    my thanks to both of you, it works fine.....so now I can go about making my millions...and when I do, CanadianJameson, I'll sort you out something a bit better than an igloo to live in ...how about a Lego house? It would be a lot more tolerant to varied weather conditions.......


    thanks again


    josie

  • #6
    Senior Coder
    Join Date
    Jul 2003
    Location
    My pimped-out igloo in Canadia
    Posts
    1,966
    Thanks
    36
    Thanked 0 Times in 0 Posts
    well, that would just be hunky-dory (errrm.. yes) but save for one thing... where would my harem of scantily-clad polar bears (et al.) move to?

    This poses major logistics issues...

    heh, if you have any more coding / design issues, you can prvate message me and i'll see what i can do. i sometimes forget to read all the posts in the forums, so just send me a quick 'hi' with a link to the thread and i'll take me a look-see

    Cheers for now, and good luck with the design.
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes :)

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The links you read are both short on detail and only discuss the less obvious problem of onload conflict which is where so many JS newbies run into trouble.

    For a short and simple script like this, making a duplicate of the script usually suffices. If you were to need three or four copies of a script then it's usually worth the time to rewrite the script so it's an object oriented script where you can use it as many times as needed. For a newbie coder though, that may be beyond you for now.

    I would say though that if you have more questions you should post them publically, and not in private messages. Public messages have the advantage os being visible to more people so you can get multiple perspectives, quicker answers and when bad advice is given you can get a warning when the giver of the bad advice is called out. If you limit yourself to private messages to a single person then you'll never know if the advice you got is the best or not.

    Private messages are where you can discuss the relative advantages of cladding polar bears and other topics less suited for public consumption.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #8
    Senior Coder
    Join Date
    Jul 2003
    Location
    My pimped-out igloo in Canadia
    Posts
    1,966
    Thanks
    36
    Thanked 0 Times in 0 Posts
    Roy Roy Roy...

    so just send me a quick 'hi' with a link to the thread and i'll take me a look-see
    as linking a thread to me which was pm'd to me would pretty much encapsulate the definition of ambiguous, I'd say that i meant that she should infact post it in the public forums (for all the valid reasons you explained above), and then just send me a pm to notify me that i should unfact go and view said public post.



    truth being told you are right on the main point though: public posts > private ones

    -- Long live the scantily-clad polar bears
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes :)

  • #9
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    And you even made my point. Because I didn't get the whole of what you said I posted based on a misunderstanding and because it was public you could call me out on it. I don't know whether to be embarassed or proud!

    Just made a run through Canada (drove the Alaska highway), I like the cooler weather up north far better than the heat down here.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #10
    Regular Coder oldcrazylegs's Avatar
    Join Date
    Feb 2004
    Location
    East Moline Illinois USA
    Posts
    414
    Thanks
    4
    Thanked 5 Times in 4 Posts
    Adding more than one script to a page http://wsabstract.com/javatutors/multiplejava.shtml

  • #11
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by josie71
    hi

    I would like to use 2 instances of your funky random image on the same page to include one image at the top and funnily enough, one at the bottom.....
    I also want to include them onto my page/s with an external .js file..ie rand1.js and rand2.js, both external files having different links/image info.

    So...my question is, can anyone give me advice as to whether there will be any conflicts such as duplicate random images/links etc?

    Thanks for your time


    Josie

    I may be mistaken but my take on this is that you want to use an external script to include several random links which use images rather than text.

    In that I detest seeing multiple instances of the same script, I put this together for you to try. Place the below script into an an external file and call it in the head of your document and then using <script> tags, call the function wherever you want a random link to appear on your document.

    This script utillizes one array and dynamicaly creates another array to store which links have already been loaded. This prevents duplicating the random links and in the event you make more random links then are contained within the array, the first link will appear as default.

    Code:
    <script type="text/javascript">
     var myNumbers = [];
      function randomLink(){// ADD YOUR IMAGES/LINKS BELOW //;
       var theLinks = [[ 'images/image0.jpg' , 'site0.html' ], 
                       [ 'images/image1.jpg' , 'site1.html' ],
                       [ 'images/image2.jpg' , 'site2.html' ],
                       [ 'images/image3.jpg' , 'site3.html' ],
                       [ 'images/image4.jpg' , 'site4.html' ],
                       [ 'images/image5.jpg' , 'site5.html' ],
                       [ 'images/image6.jpg' , 'site6.html' ],
                       [ 'images/image7.jpg' , 'site7.html' ],
                       [ 'images/image8.jpg' , 'site8.html' ],
                       [ 'images/image9.jpg' , 'site9.html' ]];
    
    
       var theNumber = Math.round(Math.random()*(theLinks.length-1));
        if(myNumbers.length < theLinks.length){
          for(var count=0; count<myNumbers.length; count++){ 
            while(theNumber == myNumbers[count]){
               theNumber = Math.round(Math.random()*(theLinks.length-1));
               count = 0;
            }
          }    myNumbers.push(theNumber);
        } else{theNumber = 0};
          var  anchor = '<a href="'+theLinks[theNumber][1]+'">';
               anchor+= '<img src="'+theLinks[theNumber][0]+'"></a>';
               document.writeln(anchor);
      }
    </script>
    </head>
    
    <body>
    <script>randomLink()</script><br>
    <script>randomLink()</script><br>
    <script>randomLink()</script><br>
    <script>randomLink()</script><br>
    <script>randomLink()</script><br>
    <script>randomLink()</script><br>
    <script>randomLink()</script><br>
    <script>randomLink()</script><br>
    <script>randomLink()</script><br>
    <script>randomLink()</script><br>
    <script>randomLink()</script><br>
    <script>randomLink()</script><br>
    <script>randomLink()</script><br>
    <script>randomLink()</script><br>
    
    
    <input type="button" onclick="alert(myNumbers)" value="Test">
    .....Willy

  • #12
    Senior Coder
    Join Date
    Jul 2003
    Location
    My pimped-out igloo in Canadia
    Posts
    1,966
    Thanks
    36
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Roy Sinclair
    And you even made my point. Because I didn't get the whole of what you said I posted based on a misunderstanding and because it was public you could call me out on it. I don't know whether to be embarassed or proud!

    Just made a run through Canada (drove the Alaska highway), I like the cooler weather up north far better than the heat down here.
    Hehe, i'd say you could pass with being "proud to be embarassed"... as it happens so rarely

    I wasn't going to call you out because
    a) you weren't directing your comments directly at me
    b) the point you were making was valid regardless of this specific context
    and c) It was a simple misunderstanding. I had the impression that you had just skimmed the post and were responding quickly, so the fangs needed no bearring

    No worries, I'll concider it a get out of CF purgatory free card for some day soon

    Cheers

    -- Jay.

    P.s, yeah, that drive would have been quite cool... you may have seen my igloo! it would be the one with the ethernet jack sticking out of it
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes :)

  • #13
    Senior Coder
    Join Date
    Jul 2003
    Location
    My pimped-out igloo in Canadia
    Posts
    1,966
    Thanks
    36
    Thanked 0 Times in 0 Posts
    willy; for the script above do you have to add both URL's and images? how would you only add one or the other?
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes :)

  • #14
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by canadianjameson
    willy; for the script above do you have to add both URL's and images? how would you only add one or the other?
    I do not understand the question. Could you please explain how you would like to use it? If for content only, just change the array and how it is referenced....

  • #15
    Senior Coder
    Join Date
    Jul 2003
    Location
    My pimped-out igloo in Canadia
    Posts
    1,966
    Thanks
    36
    Thanked 0 Times in 0 Posts
    its okay, my question wasnt important just random curiosity
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes :)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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