-2

I want to show message "New post" if there is no cookie. When you visit page four on the site, the site will set a cookie and on every other page message will be "No new posts". Problem is that this only works first time and when i deleted cookie, message was "No new posts" although there wasn't any cookie.

HTML:

<div class="news"></div>

jQuery:

function checkNews(){
    if($.cookie('cookie_name')!==null){
        $('.news').append('<div id="nonew">No new posts</div>');
    }
    else
    {
        $('.news').append('<div id="new_i">New post</div>');
    }
};

So, what is the problem and why when i delete cookie the message doesn't change to "New post"?

valek
  • 1,365
  • 16
  • 27
  • http://stackoverflow.com/questions/1458724/how-to-set-unset-cookie-with-jquery – Kamlesh Dec 13 '13 at 11:28
  • @Kamlesh Thanks, but that didn't help me to much. I've alredy known that, and my problem wasn't setting but checking the cookie. – valek Dec 13 '13 at 11:43

1 Answers1

0

I highly recommend ditching cookies altogether and using DOM Storage. Many of the limitations of cookies are solved by using sessionStorage and localStorage, including some security issues.

Jim Yarbro
  • 2,063
  • 15
  • 21
  • Solid recommendation, but should be a comment because it doesn't answer the question directly. – JJJ Dec 13 '13 at 11:34
  • 1
    I was on the fence about it, personally. On the one hand you're right, it doesn't directly answer the $.cookie() usage issue. On the other hand it answers the larger requirement of client side information storage without using the antiquated cookie technology. – Jim Yarbro Dec 13 '13 at 11:36
  • @Jamie Read Thank you but i did as Jamie Read said and it's working although i tried that before and it wasn't working. Problem solved, thanks. *He deleted his answer. – valek Dec 13 '13 at 11:37