SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member simpletex's Avatar
    Join Date
    Feb 2003
    Location
    Texas, USA
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can I change this script?

    This is a script that rotates image files based on the day of the week. Can I extend the number of files within the array to make it a month, calling them one.gif, two.gif, etc? If not, is there something else I can do to rotate images on a monthly basis?

    Many thanks from a guy who still thought java was just coffee until a few days ago.

    <script language="JavaScript">

    var days = new Array("sun.gif","mon.gif","tue.gif","wed.gif","thu.gif","fri.gif","sat.gif");

    var today = new Date().getDay();

    document.writeln('<img src="' + days[today] + '" border="0">\n');

    </script>

  2. #2
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    not sure if it's the most efficiant way, but this should work: (0 == january).

    HTML Code:
    <script language="JavaScript">
    
    var days = new Array("sun.gif","mon.gif","tue.gif","wed.gif","thu.gif","fri.gif","sat.gif" );
    var months = new Array("0.gif","1.gif","2.gif", "3.gif","4.gif","5.gif");
    
    var today = new Date().getDay();
    var todayMonth = new Date().getMonth();
    
    document.writeln(todayMonth);
    document.writeln('<img src="' + days[today] + '" border="0"> / <img src="' + months[todayMonth] + '" border="0">\n');
    
    </script>

  3. #3
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Assuming they are all gifs - there's no reason to include the extension in each array value - and doing that you can ditch the months array altogether. Do some string trickery and you can lose the days array as well. And - having done that - you can lose the other variables as well
    Code:
    document.writeln( '<img src="' + new Date().getDay().substring(0,3).toLowerCase() + '.gif" border="0"> / <img src="' + new Date().getMonth + '.gif" border="0">' );
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  4. #4
    SitePoint Member simpletex's Avatar
    Join Date
    Feb 2003
    Location
    Texas, USA
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On reading my original post I see I didn't clearly explain myself. I need it to work so that instead having seven images, there would be 30, that would change on a daily basis. Or is that what the script you wrote does. I'm so lost!

  5. #5
    SitePoint Addict
    Join Date
    May 2003
    Location
    The Netherlands
    Posts
    391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the solution beetle has given you is pretty straightforward, but you may find this one easier to understand.

    First you should make 31 pics for everyday of the month. (I'm presuming that you are using gif).

    Then you add an image to your page like:

    Code:
    <img src="" name="mypic">
    And right after it, you add a function that will choose the pic of the day

    Code:
    <script language="JavaScript">
    <!--
    function setDayPic(folderpic,picname){
    	var d=new Date();
    	var day=d.getDate();
    	var img=eval("document."+picname);
    	img.src=folderpic+"/"+day+".gif";
    }
    window.onload=setDayPic('images','mypic');
    //-->
    </script>
    Where folderpic is the folder where you have your pics stored and picname is the name you've given to the image which will rotate.

    In this example, i have pics stored in a folder called images.

    EDIT NOTE.- I forgot to add that you should call your pics "0.gif","1.gif" and so on.
    Last edited by nacho; Jun 3, 2003 at 14:34. Reason: Forgot something


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
  •