1

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!

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Garavani
  • 755
  • 1
  • 13
  • 28
  • Are you sure you don't have another Chrome window open somewhere ? Or maybe a Chrome application ? If you want Chrome to delete your cookies, you must close every single window of the browser, otherwise he will keep theme as it is still the same "session". – krtek May 12 '15 at 08:31
  • pls check http://stackoverflow.com/questions/10617954/chrome-doesnt-delete-session-cookies could be related to that setting "Continue where you left off" – Dharmang May 12 '15 at 08:46
  • @krtek: Yes, I am sure. In order to delete the cookie on Chrome I have to explicitly do so within the history deleting menu. With Safari seems to be sufficient close and reopen browser. – Garavani May 12 '15 at 08:50
  • @Dharmang: Interesting. I use the same option (open where you left) also in Safari. Indeed when I do not change the page before closing the cookie remains. But in Chrome nothing helps at all. However if someone could help me with the removeCookie stuff it could be obsolete to think about that anymore. – Garavani May 12 '15 at 08:53
  • Problem is that: $.removeCookie( 'noFadeWorks' ); is ignored completely – Garavani May 12 '15 at 09:13

1 Answers1

1

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

Fagner Brack
  • 2,365
  • 4
  • 33
  • 69
  • Wow, thanks for your detailed answer! I will switch to the new cookie plugIn as you suggested and try your solution as soon as possible. Hope this will work for me! – Garavani May 15 '15 at 05:44