Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12

Thread: JS cookie

  1. #1
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JS cookie

    Hey guys, I need help with this simple thing, I am new to js but not entirely to web design.

    I have task to make two values like this



    Code:
     
        document.cookie =
          'partizan=pogodak; expires=Sat; path=/'
         
          document.cookie =
          'zvezda=promasaj; expires=Sat; path=/'


    And now html thing, two buttons or drop down menu with this two values, partizan and zvezda

    After person click on partizan for example cookie should be stored with value pogodak and next time person come to page, it will get message pogodak and redirect to some other page, not important atm, and cookies should expire on first Saturday.

    Thanks in advance

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    You have the right idea, but the value for expires *MUST* be a real date/time string in UTC format.

    So your task here is to figure out what "first Saturday" would look like. How could it be calculated?

    Code:
    var now = new Date();
    // strip off the hours/minutes/seconds to get just the date for today:
    var theday = new Date( now.getFullYear(), now.getMonth(), now.getDate() ); 
    var wkday = theday.getDay(); // 0=sunday, 1=monday, etc.
    var daysUntilSaturday = 6 - wkday;
    if ( daysUntilSaturday == 0 ) daysUntilSaturday = 7; // on saturday, use NEXT saturday
    // and then just bump theday by that many days:
    theday.setDate( theday.getDate() + daysUntilSaturday );
    
    // and now, finally, we can use:
    document.cookie = 'partizan=pogodak; expires=' + theday.toUTCString();
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for reply, but I think task is like, anytime user visit page, it get to chose cookie, and will expire on first Saturday, for example, it can visit on monday, or on friday, but code have to do that auto, count remaining days

    and for html part, idk how to implement in html line, js code for cookie defined before

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    and will expire on first Saturday, for example, it can visit on monday, or on friday, but code have to do that auto, count remaining days
    And what do you think my code is doing there????

    Did you not see my var daysUntilSaturday ??? That is *EXACTLY* what that is for.

    And just changing the value of a cookie that was defined before will replace the prior value. You don't have to do anything special.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    aha, can u give me example of some other html code, with options and different values connected to cookies?

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    <form>
        <input type="button" id="pogodak" name="setcookie" value="Partizan" />
        <input type="button" id="promasaj" name="setcookie" value="Zvezda" />
        <!-- test button will just allow you to confirm that the cookie is actually there -->
        <input type="button" value="Test" onclick="alert(document.cookie);" />
    </form>
    <script type="text/javascript">
    var buttons = document.getElementsByName("setcookie");
    for ( var b = 0; b < buttons.length; ++b )
    {
        buttons[b].onclick = setCookie;
    }
    
    function setCookie( )
    {
        var cookieName = this.value.toLowerCase();
        var cookieValue = this.id;
    
        var now = new Date();
        // strip off the hours/minutes/seconds to get just the date for today:
        var theday = new Date( now.getFullYear(), now.getMonth(), now.getDate() ); 
        var wkday = theday.getDay(); // 0=sunday, 1=monday, etc.
        var daysUntilSaturday = 6 - wkday;
        if ( daysUntilSaturday == 0 ) daysUntilSaturday = 7; // on saturday, use NEXT saturday
        // and then just bump theday by that many days:
        theday.setDate( theday.getDate() + daysUntilSaturday );
    
        // and now, finally, we can use:
        document.cookie = cookieName + "=" + cookieValue 
                        + "; expires=" + theday.toUTCString();
    
    }
    </script>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    did u tried to run it? I add head and title just, and I get code in browser, not button, what is missing, why is reading it as code source?

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Yes, I tried it.

    Try it JUST AS I SHOW IT. Do not add anything.

    It will work.

    Now, if you add something and it doesn't work, then you made a mistake.

    Notice carefully that I did *NOT* put my JS code in the <head> section! As written, it can *NOT* go there. It should go at the end of the page, just before the </body>. In modern JavaScript, that's usually the best place to put it.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I did tried first with ur original, but same result, I get source in browser, what browser are u using?

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    It will work in all browsers.

    Here:
    http://www.plopon.com/cookie.html

    There it is on a website, *EXACTLY* as I gave it in the post.

    Just checked with Chrome, MSIE 9, and Firefox. All worked.

    Bring up that page.
    Click on the "TEST" button.
    Look at the cookie values that show there (may be none, depending on the browser).
    Then click on one of the other two buttons.
    Then click "TEST" again.
    See your cookie now listed.

    IT WORKS.

    Why it doesn't work on your site, I have no idea.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok my bad, I did save it as txt, is working thank you, am gonna work on finesse now, like instead of test I will put something like confirm, and am gonna figure out how to redirect after to some next page.

    Thanks for this code, u sure know how to code fast, ty

  • #12
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    now am geting complicated again

    I might have to put onload page for next time person come back to page, before cookie expire, and get results instant for partizan, zvezda and then get redirected to some random page www.blabla.com


  •  

    Posting Permissions

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