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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2002
    Location
    Weldon, California
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Not Random But In Order ...

    Hi All,

    I have a very elementary question for you, I'm sure, but I could not readily find an answer.

    I found the following script that will display a set of quotes randomly.

    That is not what I want, I want them to display in order.

    I want to use more than one call for this script per page, like 10 per page.

    So you see, I need them to all be listed/displayed in order.

    I plan to have well over 1000 quotes, so another question would be, that by having a script call them in order, would it be possible to continue from where it left off instead of always begining with the first quote, so that if a person views 10 pages that they will also see 100 different quotes?

    If by chance that sort of script is not possible, then I would be happy with a script that calls the quotes randomly BUT would NOT have duplicate quotes on the same page.

    I hope I made myself clear enough, and thanks for your help in advance.


    Code:
    var quotes=new Array()
    
    
    quotes[0]='blah, blah, blah 1'
    quotes[1]='blah, blah, blah 2'
    quotes[2]='blah, blah, blah 3'
    etc.,
    etc.,
    
    
    var whichquote=Math.floor(Math.random()*(quotes.length))
    
    document.write(quotes[whichquote])
    Last edited by Sharon Montero; 08-03-2002 at 12:32 PM.
    Sharon Montero
    iWomen Online Network

  • #2
    New to the CF scene
    Join Date
    Aug 2002
    Location
    Coventry , UK
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    The code below was for a news ticker that updates every 15 seconds. You can see an example by going to:

    www.milenia.34sp.com/index2.html

    Instructions: ____________________________________

    The file below is a list of headlines to be displayed in order

    <script src="http://www.milenia.34sp.com/cms/nf1.js">
    </script>

    it is in the format of:

    var mycontent=new Array()
    mycontent[0]='<a href="http://www.site.com/link1.html" target="main">News1</a>'
    mycontent[1]='<a href="http://www.site.com/link2.html" target="main">News2</a>'
    mycontent[2]='<a href="http://www.site.com/link3.html" target="main">News3</a>'


    in the HEAD section *********************************
    <script>
    var i=0
    function altercontent(){
    //if IE 4+
    if (document.all)
    {
    dcontent.innerHTML=mycontent[i];
    }
    //else if NS 4
    else if (document.layers){
    document.ns4dcontent.document.ns4dcontent2.
    document.write(mycontent[i]);
    document.ns4dcontent.document.ns4dcontent2.
    document.close();
    }
    //else if NS 6 (supports new DOM)
    else if (document.getElementById){
    rng = document.createRange();
    el = document.getElementById("dcontent");
    rng.setStartBefore(el);
    htmlFrag = rng.createContextualFragment(mycontent[i]);
    while (el.hasChildNodes())
    el.removeChild(el.lastChild);
    el.appendChild(htmlFrag);
    }
    if (i>=mycontent.length-1)
    i=0;
    else
    i++;
    }
    function beginrotation(){
    if (document.all||document.layers||document.getElementById)
    setInterval("altercontent()",7000)
    }
    window.onload=beginrotation
    </script>

    In Body Section Put: ******************************

    <div id="dcontent" class="desc"></div>
    <ilayer id="ns4dcontent"><layer id="ns4dcontent2"></layer></ilayer>

    Hope this helps

    Thanks,


    milenia

  • #3
    New to the CF scene
    Join Date
    Aug 2002
    Location
    Weldon, California
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by milenia

    Hope this helps
    Hi Milenia,

    Sorry, but that does not help me at all.

    I just want to add or replace code to the code I already have that will either display the contents in ORDER or it can display the content in random order but with NO DUPLICATES on the same page, as I will be calling that script 10 times for each Web page.

    Each call for the script must produce ONLY ONE line of content, so that I will have 10 calls for the script with only one line of content and each of them will be DIFFERENT.

    Thank you.
    Last edited by Sharon Montero; 08-03-2002 at 08:40 PM.
    Sharon Montero
    iWomen Online Network

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I just want to add or replace code to the code I already have that will either display the contents in ORDER or it can display the content in random order but with NO DUPLICATES on the same page, as I will be calling that script 10 times for each Web page.
    In order:
    http://www.codingforums.com/showthre...&threadid=3267

    -------------------

    Random order, no duplicates. To do this you must keep track of the random numbers already generated/found using a 'tracking array' that is the same size as the first array.

    If you want display ALL the values of the 'mycontents' array on every call then put in the code in color also.

    Code:
    function notNewRandom (randomNumber) (
       if (pickedNumbers[randomNumber]) == "x"
          return true;
    
       if (pickedNumbers[randomNumber) != "x" {
          pickedNumbers[randomNumber = "x";
          numbersFound++;
          return false;
       }
    } // function notNewRandom()
    
    var mycontent=new Array() 
    mycontent[0]='<a href="http://www.site.com/link1.html" target="main">News1</a>' 
    mycontent[1]='<a href="http://www.site.com/link2.html" target="main">News2</a>' 
    mycontent[2]='<a href="http://www.site.com/link3.html" target="main">News3</a>' 
    
    
    var pickedNumbers = new Array(mycontent.length)
    var whichquote = null;
    var numbersFound = 0;  // track how many different numbers we've generated
    
    // initialize the array to track the random numbers
    for (var i=0; i<pickedNumers.length; i++); {
       pickedNumbers[i] = "";
    }
    
    // keep looping until we've visited every element in 'mycontents' array
    while (numbersFound < (pickedNumbers.length)) {
       whichquote=Math.floor(Math.random()*(quotes.length))
    
       // keep generating another random number until we get one 
       // not already generated.
       while (notNewRandom (whichquote)) {
          whichquote=Math.floor(Math.random()*(quotes.length))
       }
       document.write(quotes[whichquote]);
    } // while()
    
    ------------------------------------------------------------------------
    I know what you're gonna say... "Wait, RB! This thing may run for a very long time before it finally generates all nnnnn different numbers." Not so. Trust me.
    Last edited by RadarBob; 08-04-2002 at 05:32 AM.


  •  

    Posting Permissions

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