Powered By

Powered by Blogger

Selasa, 19 Januari 2010

Fix Post Duplicates In Related Posts Widgets For Blogger

I haven't posted about Blogger in a while, but there was a certain bug in almost all the "related posts" widgets for Blogger I know, and today I finally decided to fix it for Web Upd8 and hopefully, this will also help other Blogger blogs out there.

I use this related posts widget and like most related posts widgets for Blogger, sometimes the post on which an user is currently on, is also displayed in the "related posts". This is because of the parameters in the URL (such as "?medium=" or "#disqus", etc. - basically any parameter or in-page jump link) which make it look like a different URL for the widget, thus displaying the post in the related posts section, even when the user is already on that post.

I used "replace" in javascript to fix this - which I though looks a lot like "grep" and "sed" in Linux, but it seems it's not exactly like that and I had to do a bit of hardcoding for it to work, replacing the parameters twice in the URL. I hardly know any JavaScript so don't judge me for the way I managed to fix this =).

Here is the fix. My widget has this statement:
if(a.href!=location.href)

which should have fixed the bug I am talking about, but actually it doesn't (it only fixes it for example for the Digg iframe, Stumbleupon, etc.).

So to fix it, I replaced the above piece of code with:
if(a.href!=top.location.href.replace(/\?.*/,'').replace(/\#.*/,''))

In case the above code doesn't let you save the template (you get an error), use THIS code instead.

Like I said, I hardly know any Javascript, so I replaced the parameters twice. The above code replaces "?" and "#" and anything after those parameters, with nothing, thus giving us a stripped url.

This way, the parameters (from Google Reader, etc.) and jump links such as the Disqus comments links do not interfere with the related posts widget anymore.

Limitations: make sure your posts urls don't have any "?" or "#" characters (like http://myblog.blogspot.com/my?post.html), although I think Blogger doesn't allow that anyway.

Tidak ada komentar:

Posting Komentar