Sun 26 Nov 2006
I was asked in a comment about a bit of javascript I used on DealCrew.com. Since I didn’t do anything too interesting in developing the site I figured this was as good as anything to write about.
The javascript is pretty basic, but the idea is fairly creative…although I can’t take credit for it, as I’ll explain below. The javascript simply reads a cookie and changes the css class for elements that are new since the user’s last visit. The huge advantage to this is being able to cache the page, yet still allow it to be custom for each user.
A couple caveats before I begin: 1. The javascript code used was inspired heavily by the code used at Original Signal…that is to say I took the code and modified it slightly. Which brings me to caveat #2: My modifications may have broken the code by adding inconsistencies depending on the users time zone.
On to the code:
function mark_new() {
//get the current time in milliseconds since January 1, 1970
stamp = new Date().getTime();
//get the time of the users last visit or 0 if this is their first visit
last_visit = getCookie('last_visit');
if(last_visit == null || last_visit == 0) {
last_visit = 0;
}
classes = document.getElementsByClassName('item');
classes.each(function(link) {
if(link.rel > last_visit) {
link.className = "new";
}
});
//set a cookie to time of this visit, converting to seconds from milliseconds
setCookie('last_visit', Math.ceil(stamp / 1000));
}
Check out the full code for the getCookie/setCookie functions. They do pretty much what you would guess they do.
The interesting bit is the loop through the elements with the class “item”. The first thing you’ll notice is that the getElementsByClassName function isn’t standard javascript. You’ll need to include the prototype framework. The second bit of trickery is using the rel attribute to store the timestamp for each item. I prefer seconds since the Unix Epoch, but you can use whatever format you like as long as you can get the current time in the same format in javascript. At this point it’s a simple comparison, then changing the class if the items time is newer then the last visit time.
The HTML code for the items looks like this:
Item #1
Again all credit for this should go to OriginalSignal, unless they got this from somewhere else as well.
Popularity: 4% [?]










Follow!
Email!
November 27th, 2006 at 8:31 am
Hey
Thanks for the post! I’ve tried it, but somehow, I’m still doing something wrong. This is what I did:
I’ve created 2 new files: prototype-1.4.0.js (which contains the prototype framework) and new.js (which contains the code for these functions: function mark_new, function setCookie, function getCookie.), that I have included in my html code. (so far so good)
Then I’ve made 2 css classes, a.item and a.new (they have both a different color to differentiate the links..).
Now the link looks like the one in your example (class=”item”) and also the rel=”". Now, I assume the rel needs to have the date for a specific item, so between the quotes of the rel, I’ve included a php code which gives me the date of every item (Unix time). And that’s it. Though it’s still not working. I guess I must have done something wrong?
Anyway, thanks again for helping me
November 27th, 2006 at 9:17 am
Hey Laurent,
The one thing I didn’t mention in the post is when to call the mark_new function. On DealCrew I’m using Event.Observe, which is also part of prototype, but you could also do an onload in your body tag.
If you want to post a link to your site I’d take a look at it.
November 27th, 2006 at 11:03 am
It’s just a personal rss aggregator that is located on my localhost, I don’t really have it online somewhere. Body onload did work, but I also wanted to try out the Event.Observe method, though I couldn’t figure out that one. I’ve used the same prototype.js file as your site, I also used the scriptaculous.js one, but still nothing.
November 27th, 2006 at 11:56 pm
Are you getting any javascript errors when the page loads?
November 28th, 2006 at 12:15 pm
I don’t know, but I’ve just noticed that, when using body onload (), it works fine in Firefox but in IE, all the content keeps getting bold, even after refreshing 30 min later.
November 28th, 2006 at 1:12 pm
Nice catch. I stupidly changed some code in the setCookie function that broke it in IE. I was setting the expires timestamp in the wrong format, which caused IE to reject the cookie. Fixed on the DealCrew site now.
January 16th, 2007 at 8:27 pm
Hi Matt
Thanks for tryint to explain all this in such details, however I keep having problems to get it to work…
Either the script sets all the text to bold or to the default, apparently independent from any values it gets. It never manages to set the new items only to bold.
Hitting reload even sometimes causes the page to toggle between the two css classes I have set…
I’m getting these cookie values - does that look right to you?
Name last_visit
Value 1168994943
Expires Thu, Jan 17, 2008 00:13:31
So I guess it looks like it’s setting the cookie correctly.
The onload statement looks like this:
I assume this is all I need? I don’t need to set or get a cookie here as well? I also tried your bit of Event.Observe code from DealCrew site but am not sure I’m using it correctly.
The html link I’ve got looks like this:
get_permalink() . ‘” rel=”‘ . time() . ‘” class=”item”>’ . $item->get_title() . ‘
and returns a timestamp like this, which also looks fine:
rel=”1168995025″ class=”item”
All the rest of the code I’ve got straight from your examples. scriptaculous.js and prototype.js are also present.
I’m completely new to javascript, and cookies… and very lost.
Do you have any idea what might be going wrong? I’d much appreciate it.
Thanks very much,
Oliver
January 16th, 2007 at 8:35 pm
the onLoad statement got stripped out:
body onLoad=”mark_new()”;
Thanks a lot again!
January 17th, 2007 at 12:24 am
Oliver,
Glad you were able to get it working.
January 17th, 2007 at 7:00 am
Hi Mark
thanks for your reply but sorry to disappoint, I actually didn’t… the above is as far as I got (the onLoad code got stripped out of my post above on here, not the site).
Did maybe any of the other code look suspicious to you?
I realise that you’re on to the next ’site of the month’ so if you’re busy, no worries, I’ll keep digging
January 17th, 2007 at 11:29 am
Oliver,
Nothing in the code you posted looks wrong. Is this running somewhere live that I could take a look at?
January 17th, 2007 at 12:40 pm
Thanks a lot Matt -
it’s here:
http://www.yacada.de/machine/feeddeal.php
I’ve tried all kinds of variations, but this version currently uses your exact code from above and a body onLoad=”mark_new()”; statement.
Thanks!
January 17th, 2007 at 8:45 pm
Oliver,
At first glance it seems to working. When I first go to the page all the text is red, but when I refresh the text is blue. If I clear out the cookie the text is red again.
I’ll try again later, assuming some new items are added, to see if only they are red.
January 18th, 2007 at 5:52 am
Hi Matt
yes, at first glance it looks promising, bu it’s either ‘on’ or ‘off’ - I’ve never yet had just the new items highlight. and if you keep hitting refresh, it toggles between the two states…
January 18th, 2007 at 11:43 pm
I think I see the problem. I didn’t read your code that you posted clearly enough. You want to set the “rel” attribute to the time the item was created, you’re setting it to the current time. So when your store the link in your database have a field for the time the link was stored and use that to populate the rel attribute.
January 19th, 2007 at 6:09 am
Thanks Matt
Ah, good point. I’ll give that a go and let you get on with things - if you ever have any design questions/need help, do drop me a line!
Thanks very much for your help!
Oliver