www.webdeveloper.com
Results 1 to 13 of 13

Thread: Page load/redirect based on month of year?

  1. #1
    Join Date
    Jan 2014
    Posts
    11

    Smile Page load/redirect based on month of year?

    Hi everyone ... I'm new here! Nice to find www.webdeveloper.com, and I hope it is a nice place for specific web issues and of course to learn new experience of being web developer.

    However, I'm not a javascript guy, but I can occasionally edit other code to do what I want.

    Thus, I'm trying to find code that will load a page based on the month (each one of 12 months). In other words, in January, I want 01Reception.html to load, next month in February, it changes to other web page 02Reception.htm, then over for 12 months.

    I can't find any code that refers to date ranges (without the year), but have seen some using time of month. Can that type of code be altered to fit these parameters? I'd prefer to have the page be a redirect, I guess I can work it into the menu so the correct page loads right away instead of being redirected.

    Anyone have any ideas? Thanks in advance!

    There is a copy of code, which didn't work and just show blank page. Not clue of what I missed? Will you help me please?

    <!DOCTYPE>
    <html>
    <head>

    <!-- Calendar Chart --
    01Reception.html = 0 January
    02Reception.html = 1 February
    03Reception.html = 2 March
    04Reception.html = 3 April
    05Reception.html = 4 May
    06Reception.html = 5 June
    07Reception.html = 6 July
    08Reception.html = 7 August
    09Reception.html = 8 September
    10Reception.html = 9 October
    11Reception.html = 10 November
    12Reception.html = 11 December
    -->

    <script language="JavaScript" type="text/javascript">
    function setSeason()
    {
    if(document.page)
    {
    var today = new Date();
    var month = today.getMonth()+1;
    var day = today.getDate();
    var season ='01Reception.html';
    if ((month<=2)&&(day<1)) //before March 1
    season ='02Reception.html';
    if ((month<=3)&&(day<1)) //before April 1
    season ='03Reception.html';
    if ((month<=4)&&(day<1)) //before May 1
    season ='04Reception.html';
    if ((month<=5)&&(day<1)) //before June 1
    season ='05Reception.html';
    if ((month<=6)&&(day<1)) //before July 1
    season ='06Reception.html';
    if ((month<=7)&&(day<1)) //before August 1
    season ='07Reception.html';
    if ((month<=8)&&(day<1)) //before September 1
    season ='08Reception.html';
    if ((month<=5)&&(day<1)) //before October 1
    season ='09Reception.html';
    if ((month<=5)&&(day<1)) //before November 1
    season ='10Reception.html';
    if ((month<=5)&&(day<1)) //before December 1
    season ='11Reception.html';
    if ((month<=5)&&(day<1)) //before Janurary 1
    season ='12Reception.html';
    document.page['season'].src = season;
    }

    }

    </script>
    </head>
    <body onload="setSeason()">
    </body>
    </html>

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,348
    This idea is more suited to having it written in PHP.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  3. #3
    Join Date
    Jan 2014
    Posts
    11

    :) Problem solved!

    Great news! I fixed this problem that I finally wrote the JavaScript code to redirect the web page every month!

    However, it doesn't need to have PHP file, to load web page code works out with only JavaScript in one HTML file. However, I will learn about PHP.

    Will you tell me what is the difference between JavaScript and PHP? I only know in general Javacript is client side and PHP is server side? I know very well about HTML strucutres but confused between JS and PHP.

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Lightbulb

    Quote Originally Posted by rabad View Post
    Great news! I fixed this problem that I finally wrote the JavaScript code to redirect the web page every month!

    However, it doesn't need to have PHP file, to load web page code works out with only JavaScript in one HTML file. However, I will learn about PHP.

    Will you tell me what is the difference between JavaScript and PHP? I only know in general Javacript is client side and PHP is server side? I know very well about HTML strucutres but confused between JS and PHP.
    Very difficult for other newbies to learn from your experience if you only post your problems.
    Since you did not post your solution, my assumption is that it resulted in code somewhat like this?

    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> HTML5 page </title>
    
    </head>
    <body>
    
    <script type="text/javascript">
    var Receptions = [
       '01Reception.html',   '02Reception.html',   '03Reception.html',
       '04Reception.html',   '05Reception.html',   '06Reception.html',
       '07Reception.html',   '08Reception.html',   '09Reception.html',
       '10Reception.html',   '11Reception.html',   '12Reception.html'
    ];
    window.onload = function() {
      var today = new Date();
      var mon = today.getMonth();
    //  document.location.href = Receptions[mon];       // use this for real redirection
      alert('Would be directed to: '+Receptions[mon]);  // simulated redirection for testing purposes only
    }
    </script>
    
    </body>
    </html>

  5. #5
    Join Date
    Jan 2014
    Posts
    11
    Hi JMRKER!

    Sorry by forgotting to place the answer of this code. The same code I wrote, I just deleted one line: document.page['season'].src = season;

    Then to write: window.location.href = season;

    That will work redirect webpage monthly alone!

  6. #6
    Join Date
    Jan 2014
    Posts
    11
    Hi JMRKER!

    I'm newbie too but I am learning to become web developer, which is not easier than I though.

    Sorry by forgetting to place the answer of this code. The same code I wrote, I just deleted one line: document.page['season'].src = season;

    Then to write: window.location.href = season;

    That will work redirect webpage monthly alone!

  7. #7
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377
    Your method, with all the if...statements, seemed verbose.
    So I compressed it a bit for you for a lot less typing (to reduce errors).

  8. #8
    Join Date
    Jan 2014
    Posts
    11
    Thanks JMRKER!

    I wonder how you know there are some errors with my code by many if ... statements? because I need to know there is any error in the code program like compilator for html, php, asp, etc. Did you copy this code and paste into c++ program to compile?

  9. #9
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Exclamation

    Quote Originally Posted by rabad View Post
    Thanks JMRKER!

    I wonder how you know there are some errors with my code by many if ... statements? because I need to know there is any error in the code program like compilator for html, php, asp, etc. Did you copy this code and paste into c++ program to compile?
    Whoa there! I never said your code had errors. I just said it was prone to errors when you type so much code.
    I cut the amount of code to type and gave you a solution that does what you ask in a lot less code.
    In theory it should also run faster, but with so little code involved, I don't think you will notice a speed difference.
    It will at least be faster to load without so many characters to transfer through the ether.


    As an example, when you acquire the date here...
    Code:
    var today = new Date();
    var month = today.getMonth()+1;
    var day = today.getDate();
    var season ='01Reception.html';
    if ((month<=2)&&(day<1)) //before March 1
    ...
    ...you form the month and day correctly, but the day < 1 logic is useless.
    The today.getDate() returns values of 1 to the numbers of days in the current month.
    The day value will never be less than 1, so there is no reason to check it for that condition.

    Same logic for the rest of your many if... condition statements.

    Sure, I could add more code if you wanted and still be less than your many if statements.
    Consider this:
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> HTML5 page </title>
    
    </head>
    <body>
    
    <script type="text/javascript">
    window.onload = function() {
      var today = new Date();
      var mon = today.getMonth();
      var season = '';
      switch (mon) {
        case 0 : season = '01Reception.html'; break;
        case 1 : season = '02Reception.html'; break;
        case 2 : season = '03Reception.html'; break;
        case 3 : season = '04Reception.html'; break;
        case 4 : season = '05Reception.html'; break;
        case 5 : season = '06Reception.html'; break;
        case 6 : season = '07Reception.html'; break;
        case 7 : season = '08Reception.html'; break;
        case 8 : season = '09Reception.html'; break;
        case 9 : season = '10Reception.html'; break;
        case 10 : season = '11Reception.html'; break;
        case 11 : season = '12Reception.html'; break;
      }
    //  document.location.href = season;       // use this for real redirection
      alert('Would be directed to: '+season);  // simulated redirection for testing purposes only
    }
    </script>
    
    </body>
    </html>
    I don't believe you will find much code smaller or faster than that provided in post #4.
    It is also much simpler to understand when you apply the KISS principle.

  10. #10
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    If you want to be really pesky then this:

    Quote Originally Posted by JMRKER View Post
    Code:
    <script type="text/javascript">
    var Receptions = [
       '01Reception.html',   '02Reception.html',   '03Reception.html',
       '04Reception.html',   '05Reception.html',   '06Reception.html',
       '07Reception.html',   '08Reception.html',   '09Reception.html',
       '10Reception.html',   '11Reception.html',   '12Reception.html'
    ];
    window.onload = function() {
      var today = new Date();
      var mon = today.getMonth();
    //  document.location.href = Receptions[mon];       // use this for real redirection
      alert('Would be directed to: '+Receptions[mon]);  // simulated redirection for testing purposes only
    }
    </script>
    ...could be shortened a bit more to look like this :P

    Code:
    var page = "Reception.html", month = new Date().getMonth();
            
            if (month < 10) month = "0" + (month + 1);
            location.href = month + page;

  11. #11
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Thumbs up

    Quote Originally Posted by tech_soul8 View Post
    If you want to be really pesky then this:



    ...could be shortened a bit more to look like this :P

    Code:
    var page = "Reception.html", month = new Date().getMonth();
            
            if (month < 10) month = "0" + (month + 1);
            location.href = month + page;
    Nice.
    Got so busy responding to OP with an alternate script that I forgot to analyze it the other way (even smaller).

    I think the logic for you suggestion is off just a bit for the 10th month ( when month = 9 for Oct.)
    Here is my suggested correction.
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> HTML5 page </title>
    
    </head>
    <body>
    <div id="debug"></div>
    
    <script type="text/javascript">
    
    function formFilename(month) {
      month++;
      if (month < 10) month = "0" + month;
      return (month+"Reception.html");
    }
    
    alert(formFilename(new Date().getMonth()));               // for testing purposes
    // location.href = formFilename(new Date().getMonth());   // after testing
    
    // test output simulation for all months
    var str = '';
    var mon = '';
    for (var m=0; m<12; m++) {
      str += formFilename(m)+'<br>';               // for testing purposes
    }
    document.getElementById('debug').innerHTML = str+'<p>';
    
    // original code suggestion from 'tech_soul8'
    var page = "Reception.html", month = 9;   //  new Date().getMonth();
    if (month < 10) month = "0" + (month + 1);
    // location.href = month + page;
    str += '<p>'+month+page;
    
    document.getElementById('debug').innerHTML = str;
    
    </script>
    
    </body>
    </html>
    Last edited by JMRKER; 01-29-2014 at 10:54 AM.

  12. #12
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Quote Originally Posted by JMRKER View Post
    I think the logic for you suggestion is off just a bit for the 10th month ( when month = 9 for Oct.)
    It was beta version

  13. #13
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,348
    Quote Originally Posted by rabad View Post
    Great news! I fixed this problem that I finally wrote the JavaScript code to redirect the web page every month!

    However, it doesn't need to have PHP file, to load web page code works out with only JavaScript in one HTML file. However, I will learn about PHP.

    Will you tell me what is the difference between JavaScript and PHP? I only know in general Javacript is client side and PHP is server side? I know very well about HTML strucutres but confused between JS and PHP.
    PHP works at the server, your page code can be written in to the page without any redirection.

    Javascript only works if the users browser uses javascript, PHP on the other hand makes no difference what the visitor has on or off, it will always work.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles