Links in this Post

I blogged earlier about creating a sidebar entry with all of the links in the current post. The following code snippet will do this:

 1     function createPostLinksBox(){
 2         if (!getCurrentPost()) return "not a single post page";
 3         content = getByClass("post-content")[0];
 4         links = content.getElementsByTagName("A");
 5         if (!links.length) return "no links in post";
 6         var placeholder;
 7         if (!(placeholder=getPlaceholder("outlinks"))) return "no location for outlinks";
 8         var outlinks = document.createElement('h2');
 9         outlinks.innerHTML = "Links in this Post";
10         var ul = document.createElement('ul');
11         for (i=0;i<links.length;i++){
12             li = document.createElement('li');
13             li.appendChild(links[i].cloneNode(true));
14             ul.appendChild(li);
15         }
16         replaceNode(outlinks,placeholder);    
17         outlinks.parentNode.insertAfter(ul,outlinks);
18     }

Note that this must be run after the DOM tree is finalised, and relies on a couple of other functions. I’ve put <span id="outlinks">``</span> into my template, so that this will appear wherever that is, if it’s a single post page with any links in it. It will make it into the next update of my script.

blog comments powered by Disqus