简体   繁体   中英

Trouble with setting jQuery cookie

First of all, sorry, I see there are lots of repetitions about the whole cookie question, yet I have to add this one for I am getting quite confused with different jQuery cookie plugIns.

I am using this jQuery Cookie Plugin v1.4.1

I successfully implemented the following code on one of my pages (not the home page!)

$(document).ready( function() { 
    if ($.cookie('noFadeWorks')) {
        // do something when initial effect on page enter has been already seen
    } else {
        // functions here to do the initial page enter effect
        $.cookie( 'noFadeWorks', true );    
    };
});

Now this basically works with some problems/questions yet:

  1. Working as expected in Safari. When browser is closed and reopened everything starts from 0 as expected and desired. YET: Google Chrome even after close and reopen still has the cookie. My effect functions do not enter the game anymore. Not desired here.

  2. It would be awesome if from some points of the session I could tell the browser to forget about the cookie and start from 0 (with the effect on the certain page) again. I tried for that: $.removeCookie( 'noFadeWorks' ); on my homepage but this didn't work at all.

Do I explain myself? How is this to be done correctly? I also tried the expire options without success.

Thanks so much in advance!

Note: jquery cookie v1.4.1 does not accept a Number as value: https://github.com/js-cookie/js-cookie/tree/v1.4.1 . But that is not the problem.

I have created a small test case with jquery v2.1.4 and jquery-cookie v1.4.1 (from the link above):

<!DOCTYPE html>
<script src="jquery.js"></script>
<script src="jquery-cookie.js"></script>
<script>
    $(document).ready( function() { 
        if ($.cookie('noFadeWorks')) {
            // do something when initial effect on page enter has been already seen
            alert( "has cookie!" );
        } else {
            // functions here to do the initial page enter effect
            $.cookie( 'noFadeWorks', 'true' );
            alert( "No cookie, creating..." );
        };
    });
</script>

The following occurs:

  1. When you enters the first time, it creates the cookie ("No cookie, creating...")
  2. When you refresh the page, the cookie exists ("has cookie!")
  3. If you close and open the browser again, it says ("has cookie!"), the expected result is ("No cookie, creating...") because in v1.4.1 it should create a session cookie by default and we didn't specified path: "/"

If I use ctrl + shift + n to start a window in anonymous mode the cookie is removed (Chrome start a new clean browser instance).

So here is my theory:

If you have selected to restore the tabs after reopening Chrome, then the cookie is created again because Chrome is restoring it's window session, including all the cookies that existed previously. Probably if you disable the "restore session" feature of chrome, the cookies will be removed once the user closes the browser, as expected.

I didn't tested disabling that feature, but intuitively that seems to be the problem.

It would be awesome if from some points of the session I could tell the browser to forget about the cookie and start from 0 (with the effect on the certain page) again.

You can specify a path in which you want the cookie to be valid, the cookie will not be possible to be read from another path closer to the root (like / ):

<!DOCTYPE html>
<script src="jquery.js"></script>
<script src="jquery-cookie.js"></script>
<script>
    $(document).ready( function() {
        if ($.cookie('noFadeWorks')) {
            // do something when initial effect on page enter has been already seen
            alert( "has cookie!" );
        } else {
            // functions here to do the initial page enter effect
            $.cookie( 'noFadeWorks', 'true', {
                // It will not be visible in the root, only in pages inside the "/dir/" path
                path: "/dir/"
            });
            alert( "No cookie, creating..." );
        };
    });
</script>

I recommend to try out the latest version of jquery-cookie (now js-cookie): https://github.com/js-cookie/js-cookie/releases

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM