1

Today I decided to get to understand cookies better. The easiest way seemed jQuery. I used carhartl's Cookie plugin.

Here's a fiddle.

Basically, what I want is: when a user clicks the 'X', the pop-up closes (works fine) and a cookie is written so that the next time that user hits that page, he doesn't see the pop-up again!

function showPopUp() { var popUpMT = -($("#popUp").height() / 2) - 50; $("#popUp").css("marginTop", popUpMT).fadeIn("slow"); $("#overlay").fadeIn("fast"); } function setCookie() { $("#popUp").fadeOut("fast"); $("#overlay").fadeOut("fast"); $.cookie("popUpCookie", { expires: 365 }); } $(document).ready(function() { var ourCookie = $.cookie("popUpCookie"); if (ourCookie === null) { showPopUp(); } $("#popUp span#close").click(function() { setCookie(); }); }); 

1 Answer 1

1

In your setCookie function, add some value as second argument as shown in the documentation.

$.cookie("popUpCookie", "true", { expires: 365}); 

In your fiddle, you only pass in the name and a settings object. Other than the it should work and does for me in this update.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I did not know a value was obligatory. Also +1 for a more logical function order!
See line 24 of the cookie plugin. Only if the value argument is given and not an object, the code to set the cookie is executed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.