2005-10-09

Blogger backlinks the custom way

Blogger now offers backlinks. It's a sort of trackback system, but not quite.

Blogger has put a help article online on how to enable backlinking. Read it if you want to use the non custom Blogger version of backlinking. If you want custom backlinking just paste these bits of code to your template (you don't need to make the settings described in the help article).

On your MainOrArchivePage (after the link to the comments for example):

<script type="text/javascript">
//<![CDATA[
if (! BL_BacklinkCount) var BL_BacklinkCount = new Array();
BL_BacklinkCount['<$BlogItemNumber$>'] = document.createElement('span');
//]]>
</script>
<script type="text/javascript" src="http://www.blogger.com/dyn-js/backlink_count.js?blogID=<$BlogID$>&postID=<$BlogItemNumber$>"></script>
<script type="text/javascript">
//<![CDATA[
document.write(
  ' &middot; <a href="<$BlogItemPermalinkUrl$>#backlinks">Backlinks: '+
  BL_BacklinkCount['<$BlogItemNumber$>'].innerHTML +'</a>'
);
//]]>
</script>

Update 2006-01-07: I did a rewrite to speed this part up.

On your ItemPage (after the comments for example):

<script type="text/javascript">
//<![CDATA[
var BL_Backlinks = new Array();
//]]>
</script>
<script type="text/javascript" src="http://www.blogger.com/dyn-js/backlink.js?blogID=<$BlogID$>&postID=<$BlogItemNumber$>"></script>
<script type="text/javascript">
//<![CDATA[
if (BL_Backlinks && BL_Backlinks.length) {
  document.write('<div id="backlinks"><a name="backlinks"></a><h3>Backlinks</h3><dl>');
  for (var i = 0; i < BL_Backlinks.length; i++) {
    document.write(
      '<dt><a href="'+ BL_Backlinks[i]["BlogBacklinkURL"] +'">'+ BL_Backlinks[i]["BlogBacklinkTitle"] +'</a></dt>'+
      '<dd>'+ BL_Backlinks[i]["BlogBacklinkSnippet"] +'<a name="t'+ i +'"></a><p>'+
      BL_Backlinks[i]["BlogBacklinkAuthor"] + ' &middot; '+ BL_Backlinks[i]["BlogBacklinkDateTime"] +'</p></dd>'
    );
  }
  document.write('</dl></div>');
}
//]]>
</script>

Here is an example page with backlinks.

13 comments:

Anonymous said...

promising, I'll give it a go!
thanks for pointing me to your site
all best

Anonymous said...

Great work! I don't know whether you intended it, but you've actually managed to fix a major bug in Blogger's release, thus significantly improving upon the original.

Anonymous said...

I just wanted to add my thanks to Richard's. Not only you fixed that horrible bug, but this linkback looks a lot better than the offical release.

Anonymous said...

Hi there - I don't really understand javascript at all, but I am really good at following instructions, and I wondered if you could post a slight change to your script for how to display backlinks on the same page? For example, I use the Peek-A-Boo comment script, and I would like my BackLinks to also "Peek-A-Boo" on the same page... Thanks so much (in advance)

Anonymous said...

Jasper,

I've tried using this linkback script and, without fail, it never appears on my item pages. I clicked on the links above and found that these blog, which claim to have enabled it, also don't have it. I think it's only working on Browservulsel and Testvulsel, and I can't figure out why.

Has Blogger's script moved or something?

Anonymous said...

This script depends on a Javascript provided by Blogger. It hasn't moved and this trick should work on all blogspot blogs.

Anonymous said...

How simple and robust it became if one didn't bother with the custom foldable bits Blogger offer for folding and unfolding the backlinks, which I took to doing of my own with what little Blogger offers in scripted provisions for that. I really ought to stop considering document.write being cheating; it's much easier relying on that than working around it as I do.

Anonymous said...

Jasper,

Backlinks are doing something funny tonight!! All of my posts still have the link in the footer, but only a few have the count, whether a 0 or a positive number. Is this a Google error, or has something changed that impacts the hack?

Thanks

John

Anonymous said...

@John: I've just checked your blog and everything seems to be working at the moment. Blogger.com might have been to busy to serve the backlink_count.js script. I'll keep an eye on it.

Anonymous said...

Thanks... sorry to bug you with my junk!!

Anonymous said...

Thanks much, Jasper. I've got backlinks up and running, and better yet, they don't conflict with my "peek-a-boo" comments like the Blogger hack did.

Anonymous said...

thanks

tsetsao said...

thanks