Reputation: 785
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:
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.
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!
Upvotes: 1
Views: 1395
Reputation: 2403
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:
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
Upvotes: 1