Zel Zel - 2 years ago 77
Javascript Question

Where's my Cookie?

I need to add a class ‘slow’ to a div with class ‘slide’, but only once – the first time in the session. I am trying to achieve that with the help of jquery.cookie.js (version 1.4).

So far I came up with this:

if ($.cookie('biscuit') !== 'first') {
$(".slide").addClass( "slow" ), function() {
$.cookie('biscuit', 'first');


In other words, I am checking does cookie named ‘biscuit’ has a value ‘first’. If not – I add the class ‘slow’ (which does CSS3 transition, but that’s beyond the scope of this exercise) and after that – I set the cookie ‘biscuit’ with the value ‘first’, so when the user returns to the page (within ongoing session) the script doesn’t add the class.

Unfortunately, the cookie never gets set and the class is added (within the same session) again and again. Why the cookie isn’t there? Where’s my biscuit?

Answer Source

There is no callback in addClass, so simply do it like this:

if ($.cookie('biscuit') !== "first") {
    $.cookie('biscuit', 'first');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download