SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript Month/Date Loading, plz help

    I have a website with all of the months of the year. The main page of the website is suppose to be the current month. How can make my index page load automatically to the correct month. So if it is April, my index page will automatically load april.html.
    Thank you so much!

  2. #2
    SitePoint Guru Jason__C's Avatar
    Join Date
    Oct 2009
    Location
    Racoon City
    Posts
    660
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code JavaScript:
     
    <script language="JavaScript">
    <!-- Hide from older browser
    var x= new Date();
    var m= x.getMonth()+1  ;
    document.location.href =" + m + .html";
    //-->
    </script>

    You can also use a switch:
    Code JavaScript:
    <script type="text/javascript">
    var dateobj=new Date()
    var month=dateobj.Month() + 1;
    switch(month){
    case 1:
    window.location="January.htm"
    break
    case 2:
    window.location="February.htm"
    break
    case 3:
    window.location="March.htm"
    break
    case 4:
    window.location="April.htm"
    break
    }
     
    </script>

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by dseg42 View Post
    ISo if it is April, my index page will automatically load april.html.
    Thank you so much!
    The best way to do that would be with server side scripting - that way the correct page will load for all the people with javaScript disabled or not available as well as those where it is available.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,789
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by USPaperchaser View Post
    Code JavaScript:
    <!-- Hide from older browser
    You should be more specific about which browsers you are hiding the script from when you include that code.

    The older browsers it refers to are Internet Explorer 2, Netscape 1, and the browsers that preceded them.

    The comment doesn't hide the script from Internet Explorer 3 or Netscape 2 or more recent browsers than those.

    At the current rate of browser development it should be possible to do away with such comments once everyone stops using Netscape 1 and IE2 - which will most likely happen long before the end of the 20th century - say 1998 to play it safe.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Guru Jason__C's Avatar
    Join Date
    Oct 2009
    Location
    Racoon City
    Posts
    660
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Alright man, I'm not the best at JS, but regardless off the comments I added, to hide from older browsers, the rest will still work. Just giving the guy a jump-start, which he can build from. Not a copy-and-paste. However, I didn't take into account using a server-side solution, as this post is in the JS Forum.

  6. #6
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow thank you so much! I used the first example and i have another question. I know this is the JS forum but how do I make a html file open another html file in the same directory?


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
  •