SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: ordered numbers

  1. #1
    SitePoint Member
    Join Date
    Feb 2003
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ordered numbers

    im trying to find a script that can cycle through a list of numbers in order. the closest script that i found on the net was this one, but it cycles through the numbers randomly-

    <script language="javascript">
    <!--
    var number = new Array()

    number[1] = "01"
    number[2] = "02"
    number[3] = "03"

    var rn = Math.floor(Math.random() * number.length)
    if (rn == 0) {
    rn = 1
    }
    document.write(""+number[rn]+"")
    //-->
    </script>

    i need a script like that, but not random. can someone help me?

  2. #2
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: ordered numbers

    Originally posted by dande
    im trying to find a script that can cycle through a list of numbers in order.
    i need a script like that, but not random. can someone help me?
    Code:
    <script language="javascript">
    <!-- 
    var number = new Array()
    
    number[1] = "01"
    number[2] = "02"
    number[3] = "03"
    
    var rn = 1;
    for(var index=0; index<number.length; index++)
    {
        document.write(number[rn]);
    }
    //-->
    </script>
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  3. #3
    SitePoint Member
    Join Date
    Feb 2003
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Re: ordered numbers

    Originally posted by dale_burrell
    Code:
    <script language="javascript">
    <!-- 
    var number = new Array()
    
    number[1] = "01"
    number[2] = "02"
    number[3] = "03"
    
    var rn = 1;
    for(var index=0; index<number.length; index++)
    {
        document.write(number[rn]);
    }
    //-->
    </script>

    thanks for the help! but this code produces "01010101" and doesn't change when i refresh the page. what i'm trying to do is make a new number appear every time the page is refreshed, going from "01" to "03" (in order).

  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where it says number[rn], use number[index+1] instead, to get the number.

    But as I understand it you want a single number, each time the page is refreshed. That is gonna be a little more complicated, as you need to store the number that was displayed last time, which involves sessions, or passing the current page count via parameters. Not something you really want tackle with JavaScript alone.
    Last edited by mubs; Feb 7, 2003 at 19:59.

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All you need to do is store the last number in a cookie. If the user has their cookies turned off, they would always see just the first number.

    Here's a link to some code that does this...
    http://travis.squidfingers.com/orderednums.html

    Code:
    <html>
    <head>
    <title></title>
    <script type="text/JavaScript">
    
    // --------------------------------------------------
    
    var numbers = [
    	"Number: 1",
    	"Number: 2",
    	"Number: 3",
    	"Number: 4",
    	"Number: 5"
    ];
    
    // --------------------------------------------------
    
    function setCookie(name,value,days){
    	if(days){
    		var date = new Date();
    		date.setTime(date.getTime()+(days*24*60*60*1000));
    		var expires = "; expires="+date.toGMTString();
    	}else{
    		var expires = "";
    	}
    	document.cookie = name+"="+value+expires+"; path=/";
    }
    function getCookie(name){
    	name += "=";
    	var s = document.cookie.split("; ");
    	for(var i=0; i<s.length; i++){
    		var c = s[i];
    		if(c.indexOf(name) == 0){
    			return unescape(c.substring(name.length,c.length));
    		}
    	}
    	return null;
    }
    cookieIndex = getCookie("lastIndex");
    index = cookieIndex ? parseInt(cookieIndex)+1 : 0;
    if(index > numbers.length-1) index = 0;
    setCookie("lastIndex",index,365);
    
    </script>
    </head>
    <body>
    
    <script type="text/JavaScript">
    	document.write(numbers[index]);
    </script>
    
    </body>
    </html>
    travis

  6. #6
    SitePoint Member
    Join Date
    Feb 2003
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks podlob! thats perfect!

    actually, is there a way to have the script write out something like <img src="01.jpg"> so that when the page is refreshed, it'll show the next image <img src="02.jpg"> (and 03.jpg and so on)?

    i tried playing around with the document.write line, but it wouldn't produce the tag (the images didn't show up). i'm asking because i can't run cgi or php or anything like that on my server, and i want to be able to cycle through images on my page.

    thanks a bunch!
    Last edited by dande; Feb 8, 2003 at 14:02.

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here's how you would do it in php (I just read that you can't use php, but I'll post it anyway.)

    http://travis.squidfingers.com/orderednums.php

    Code:
    <?php
    
    $numbers = array (
    	"Number: 1",
    	"Number: 2",
    	"Number: 3",
    	"Number: 4",
    	"Number: 5"
    );
    
    $index = (isset($index)) ? $index+1 : 0;
    if($index > count($numbers)-1) $index = 0;
    setcookie("index", $index, 0, "/");
    
    ?>
    <html>
    <head>
    <title></title>
    </head>
    <body>
    
    <?php
    	echo $numbers[$index];
    ?>
    
    </body>
    </html>
    Last edited by podlob; Feb 8, 2003 at 14:32.
    travis

  8. #8
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's how you could do it with images...

    http://travis.squidfingers.com/orderednums/

    Code:
    <html>
    <head>
    <title></title>
    <script type="text/JavaScript">
    
    // ||||||||||||||||||||||||||||||||||||||||||||||||||
    
    function setCookie(name,value,days){
    	if(days){
    		var date = new Date();
    		date.setTime(date.getTime()+(days*24*60*60*1000));
    		var expires = "; expires="+date.toGMTString();
    	}else{
    		var expires = "";
    	}
    	document.cookie = name+"="+value+expires+"; path=/";
    }
    function getCookie(name){
    	name += "=";
    	var s = document.cookie.split("; ");
    	for(var i=0; i<s.length; i++){
    		var c = s[i];
    		if(c.indexOf(name) == 0){
    			return unescape(c.substring(name.length,c.length));
    		}
    	}
    	return null;
    }
    
    // ||||||||||||||||||||||||||||||||||||||||||||||||||
    
    var imgList = [
    	"img1.gif",
    	"img2.gif",
    	"img3.gif"
    ];
    
    cookieIndex = getCookie("lastIndex");
    index = cookieIndex ? parseInt(cookieIndex)+1 : 0;
    if(index > imgList.length-1) index = 0;
    setCookie("lastIndex",index,365);
    
    // ||||||||||||||||||||||||||||||||||||||||||||||||||
    
    </script>
    </head>
    <body>
    
    <script type="text/JavaScript">
    	document.write('<img src="'+imgList[index]+'" width="100" height="100">');
    </script>
    
    </body>
    </html>
    travis


Bookmarks

Posting Permissions

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