0

i want to highlight new posts since the user's last visit, just as it is explained here:

How to highlight new items on the site since last visit with jquery?

i use this jquery to filter the links i want to highlight - it works perfectly fine

$(".itemlink").filter(function() {
    return  $(this).attr("data-timestamp") > $.cookie("last_visit");
}).addClass('highlight');

but i don't know when is the right time to set the "last-visit" cookie.

so far, i tried this:

$(window).unload(function(){
    $.cookie("last_visit", $.now());
});

it does quite what i expected it would - sadly if the page is refreshed, of course that is an "unload" event as well... so when i refresh the page, i won't see any highlighted elements, because the "last-visit" cookie reads $.now()

any good ideas for a better time/way to set the "last-visit" cookie?

Community
  • 1
  • 1
Matthias Schmidt
  • 585
  • 1
  • 7
  • 25

1 Answers1

0

You could create a session cookie when you update the last_visit value. Then, check if the session cookie exists, and only update the last_visit value if it doesn't. That way, it won't be updated on refresh, but it will be updated when the browser is closed and re-opened (because the browser will delete the session cookie).

Alex W
  • 37,233
  • 13
  • 109
  • 109