0

can't seem to get this code to work, its not throwing errors, but something just isnt right as its not saving the cookie.

Basically on click i need it to check if the cookie is active, if not it needs to add it. This is remove a popup on the website.

$('.close-reveal').on(click, function(){
        // if no cookie
        if (!$.cookie('alert')) {
            $('.subscribe-scroller').hide();
            // set the cookie for 24 hours
            var date = new Date();
            date.setTime(date.getTime() + 24 * 60 * 60 * 1000); 
            $.cookie('alert', true, { expires: date });
        }

});
  • http://stackoverflow.com/questions/2824021/create-a-cookie-if-and-only-if-it-doesnt-already-exist – Adrianopolis May 12 '17 at 21:52
  • Thanks Andrianopolis, jquery is not my strength, so that link you sent makes no sense to me :( – Pieces of Eight May 12 '17 at 21:53
  • Is this the lib you're using? https://github.com/carhartl/jquery-cookie I haven't tested this, but I suspect your problem is the attempt to set a boolean value to a cookie. If I'm right about that being the lib, read this bit about JSON in the docs: https://github.com/carhartl/jquery-cookie#json – JAAulde May 12 '17 at 21:53
  • Ye thats the library, will ready that now, thanks! – Pieces of Eight May 12 '17 at 22:00
  • None of this makes sense to me :( how can i apply this to my code above? – Pieces of Eight May 12 '17 at 22:01
  • Try adding this line of code, before all the code you showed us here: `$.cookie.json = true;` – JAAulde May 12 '17 at 22:02
  • Unless you really need specifically cookies, you should use localstorage instead. It's much easier to work with. – JJJ May 12 '17 at 22:02

1 Answers1

0

You need the quotes around the true.

Try this:

$('.close-reveal').on(click, function(){
        // if no cookie
        if (!$.cookie('alert')) {
            $('.subscribe-scroller').hide();

            $.cookie('alert', 'true', { expires: 1 });
        }

});

Cookies are only string-valued. If you want to treat the value as a boolean, you need to parse it back to a boolean when the cookie value is read back out.

Secondly why making the effort of calculating one day if you can simply enter 1 for one day?

Tom el Safadi
  • 6,164
  • 5
  • 49
  • 102