SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict caser85's Avatar
    Join Date
    May 2002
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Use javascript to redirect page depending on month

    Ok, here is what I am trying to do. I have a folder with separate pages for each month of the year. For example, they are called: september2002.html, octobober2002.html, etc. What I want to do is create an index.html file in the calendar folder that redirects to the right month and year page depending on the date. So when someone goes to www.mysite.com/calendar and it is july, 2002, it will forward them to www.mysite.com/calendar/july2002.html.

    Anyone have any ideas or links to get me started?

    Thanks!

    Casey

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use

    intMonth = today.getMonth();

    to get the month, then (bare in mind that it will return 0 to 11) use if statements to handle the window.location, eg

    if(intMonth == '0') {
    window.location = 'january2002.html';
    }
    if(intMonth == '1') {
    window.location = 'febuary2002.html';
    }
    .
    .
    .
    .
    .
    .
    .

    the best way would be to use case ifs however I can't remember if Java Script has them.

    or

    an alternative which would save the logic statements would be to do something like this :

    intMonth = today.getMonth();
    intYear = today.getFullYear();

    window.location = months[intMonth] + intYear + '.html';

    where months is an array with all your months in.
    Last edited by Lunchy_Munchy; Jul 16, 2002 at 15:41.

  3. #3
    SitePoint Addict caser85's Avatar
    Join Date
    May 2002
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Lunchy_Munchy

    an alternative which would save the logic statements would be to do something like this :

    intMonth = today.getMonth();
    intYear = today.getFullYear();

    window.location = months[intMonth] + intYear + '.html';

    where months is an array with all your months in.
    Thanks for your help. I decided to put it in an array. I had look all over the web to do this, put I found it! Here is the code I ended up using:

    Code:
    <!-- Begin
    var Today=new Date();
    var ThisDate=Today.getDate();
    var ThisMonth=Today.getMonth()+1;
    var ThisYear=Today.getFullYear(); 
    
    function MonthTxt (MonthNumber) {
    var Month=new Array();
    Month[1]="january";
    Month[2]="february";
    Month[3]="march";
    Month[4]="april";
    Month[5]="may";
    Month[6]="june";
    Month[7]="july";
    Month[8]="august";
    Month[9]="september";
    Month[10]="october";
    Month[11]="november";
    Month[12]="december";
    return Month[MonthNumber];
    }
    var MonthName=MonthTxt(ThisMonth);
    
    if ((MonthName == "june") || (MonthName == "july") || (MonthName == "august")) 
    
           {
             window.location = "http://www.midwestmarket.net/rotc/new/cadets/calendar/september"+ThisYear+".html";
           }
          else
           {
             window.location = "http://www.midwestmarket.net/rotc/new/cadets/calendar/"+MonthName+ThisYear+".html";
           }  
          
    //  End -->
    As you can see, if the month is June, July, or August, I direct to September of that year because we do not have school during the summer and nothing is going on. If you want to try out the results go here: http://www.midwestmarket.net/rotc/new/cadets/calendar

    Thanks again for your help!

  4. #4
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why declare the array like that - you're just using way too many bytes:

    var Month=new Array();
    Month[1]="january";
    Month[2]="february";
    Month[3]="march";
    Month[4]="april";
    Month[5]="may";
    Month[6]="june";
    Month[7]="july";
    Month[8]="august";
    Month[9]="september";
    Month[10]="october";
    Month[11]="november";
    Month[12]="december";

    What's wrong with

    var Month = new Array('','january','february',...);

    that'd save about 90 characters and just make it tidier, wouldn't it?

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---


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
  •