1

I have a couple of divs on the site. I'd like to have the new items highlighted since the last visit.

I don't want to use php and mySQL for this, because: - i don't want to store every users last visit info in the database - i want this to work with non registered/non logged in users too - the main content is cached, so i can't output different content to different users.

I think this leaves me one solution: cookies + javascript: - check if cookie exists - if yes highlight new items (example add a class to the div) - update cookie

So, how can I do this?

The divs are currently using the same class, and they have no id, so if needed I can put there the date of the div, or the primary ID of the item.

ZTefter
  • 189
  • 1
  • 2
  • 11
  • Is your question about "how to highlight" or "how to read/write cookies?" – PhD Jul 01 '12 at 04:23
  • Highlighting is easy with adding a class. My problem is how to separate the new divs from the old ones with cookies? And doing all this new items highlighting on the client side. – ZTefter Jul 01 '12 at 04:31
  • You'll need to store `div id` and it's associated `timestamp` of creation in the cookie. Think of it like a `hashmap` like structure – PhD Jul 01 '12 at 04:40
  • In theory this would be a fine solution, but how to code it? $("div.items" [where timestamp > timestamp in cookie]).addClass("newitems") – ZTefter Jul 01 '12 at 04:55

1 Answers1

1

You can add a data-timestamp="[current_timestamp]" attribute to your divs. Then store the Timestamp of the last visit in a cookie and add a class like this

$('.divClass').filter(function() {
  return $(this).attr("data-timestamp") > "[last_visit_timestamp]";
}).addClass('highlight');

(see jQuery: Selecting all elements where attribute is greater than a value)

You can easily read/write cookies with: https://github.com/carhartl/jquery-cookie/

Community
  • 1
  • 1
gherkins
  • 14,603
  • 6
  • 44
  • 70