SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2002
    Location
    Wiltshire, England
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Link only works after a date

    Hey Guys

    Please bare with me as im crap when it comes to javascript aint really done much with it atall.

    I want to put a advent calander type thing on my website with 24 links.
    When a use clicks on link it takes them to that page but only if the day is gone, so on the 3rd they can click on the 1st, 2nd and 3rd link with no problem and if they click on the 4th it pops up a message box telling them to come back 2morrow.

    How do dates work in javascript like i said i havnt really done anything in javascript before

  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)
    This should do something like what you want. I haven't added a test for it being one day in the future - I'm sure you can work the logic from here...

    <script>
    function TestDate()
    {
    var LinkDate = new Date(this.id);
    var Today = new Date();

    // Probably more concise ways of doing this - but this should be clear what is happening.
    if( (LinkDate.getYear() < Today.getYear()) ||
    (LinkDate.getYear() == Today.getYear() && LinkDate.getMonth() < Today.getMonth()) ||
    (LinkDate.getYear() == Today.getYear() && LinkDate.getMonth() == Today.getMonth() && LinkDate.getDate() <= Today.getDate() )
    )
    {
    return true; // Means link will operate
    }
    else
    {
    alert("Sorry that date hasn't passed yet");
    return false; // link will do nothing.
    }
    }
    </script>

    <!-- Somehow you have to know what date a link refers to, one possible way is to make that id the date it represents -->
    <a id="DateOfLink" href="blah.blah" onClick="return TestDate(this);">
    Last edited by dale_burrell; Nov 28, 2002 at 05:31.
    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 Zealot
    Join Date
    Jun 2002
    Location
    Wiltshire, England
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers mate it seems fine when i force dates into the variables to test it but i get a little problem when using it properly

    This is my link

    <a id="01/12/2002" href="test.asp?link1" onClick="return TestDate(this);">

    Then i added this to the top to show me the dates its using

    alert(LinkDate);
    alert(Today);

    But the first alert box returns NaN
    Do you know why that is

  4. #4
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, now you're run into the msot fun thing about JavaScript - date formats.

    The trusty old book sayd that a Date can be created using the following date formats

    - ("month, dd, yyyy hh:mm:ss")
    - ("month, dd, yyyy")
    - ("yy, mm, dd, hh, mm, ss")
    - ("yy, mm, dd")

    I don't think thats totally true anymore, I think it might accept a few others, and might not need the commas.

    dd mmm yyyy (i.e. 12 Apr 2002)

    if not I suggest April 12 2002

    Anyway, try a different format and see what you get.
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2002
    Location
    Wiltshire, England
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ive tryed loads of date combos at it still aint aving it,
    got any other suggestions

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

    Opps

    Sorry mate - I never tested it.

    try...

    <html>
    <head>
    <title>Login</title>
    <script>
    function TestDate(MyLink)
    {
    var Temp = MyLink.id.replace(/_/g, " ");
    var LinkDate = new Date(Temp);
    var Today = new Date();

    alert(MyLink.id + ' ' + LinkDate);

    // Probably more concise ways of doing this - but this should be clear what is happening.
    if( (LinkDate.getYear() < Today.getYear()) ||
    (LinkDate.getYear() == Today.getYear() && LinkDate.getMonth() < Today.getMonth()) ||
    (LinkDate.getYear() == Today.getYear() && LinkDate.getMonth() == Today.getMonth() && LinkDate.getDate() <= Today.getDate() )
    )
    {
    return true; // Means link will operate
    }
    else
    {
    alert("Sorry that date hasn't passed yet");
    return false; // link will do nothing.
    }
    }
    </script>
    </head>
    <body bgcolor="#faf6ea">

    <!-- Somehow you have to know what date a link refers to, one possible way is to make that id the date it represents -->
    <a id="1_sept_2002" href="blah.blah" onClick="return TestDate(this);">Ha ha ha</a>

    </body>
    </html>
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  7. #7
    SitePoint Zealot
    Join Date
    Jun 2002
    Location
    Wiltshire, England
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers Mate thats great,
    thankyou very much


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
  •