30 How to create a table of contents page for your blog
October 09, 2007 /

Until recently, I used Beautiful Beta's excellent "Table of contents" hack for this blog. This technique loaded the table of contents above other posts, and was accessible from a link in the sidebar. The only downside of this hack is that it significantly slows blog loading time, especially when your blog has more than 100 posts.

For the new template, I have modified this hack to show the table of contents for Blogger Buster on a post page instead. In this post I will explain how I did this.

Beautiful Beta's TOC hack requires two components. There is the code which calls the contents of your blog, and provides a link in the sidebar:

<div id="toclink"><a href="javascript:showToc();">Show TOC</a><br/><br/></div> <script style="text/javascript" src="http://home.planet.nl/~hansoosting/downloads/blogtoc.js"></script> <script src="http://YOURBLOG.blogspot.com/feeds/posts/default?alt=json-in-script&max-results=999&callback=loadtoc"></script>
And the div element which features in a widget above your blog posts:

    <div id="toc"></div>

We need to combine both of these elements inside a post page, which will then create the link to show the contents of your blog, and open this in the actual post page.

First, create a new blog post and edit this in HTML mode, instead of rich text compose mode. Then, copy both elements of code from Beautiful Beta's hack into this page, like this:

Don't forget to change "YOURBLOG.blogspot.com" to the actual URL of your blog!

Then simply save the page, and you are done!

If you have more than 100 posts in your blog, you will need to repeat the line which calls the posts feed from your blog, modifying this so that it begins from the 101st post, like this:

<script src="script src="http://YOURBLOG.blogspot.com/feeds/posts/default?alt=json-in-script&start-index=1&max-results=100&callback=loadtoc"></script> <script src="http://YOURBLOG.blogspot.com/feeds/posts/default?alt=json-in-script&start-index=101&max-results=100&callback=loadtoc"></script>
If you have more than 200 posts, you will need to add extra lines where the start-index is 201, and so on.

This ensures all of your posts are included in the original table of contents.

IMPORTANT: if you have already installed the TOC hack in your sidebar and above the main posts column, you will need to delete these two widgets to ensure they don't interfere with the loading of the table of contents on your post page, and also slow down blog loading time!

How it works

The TOC hack uses javascript to call the post titles from your blog's feed, and then displays them in a tabular format, arranged by post title, date or label.

This version of the TOC hack works almost exactly the same as Beautiful Beta's: the reader must click on the link to display the whole table of contents in the page, and is shown a message to explain that the page is loading. The main advantage of using a post page to display the TOC is that page loading time is significantly reduced for the whole blog, and only the TOC post page had a longer loading time.

I would have preferred to have the TOC load instantly with the page, but after some experimentation, I believe that I would need to add an "onload" function to the head of my template, which would load the TOC along with the rest of the blog even though it is only displayed on one page. However, I have tried to contact Beautiful Beta to verify this, and will of course post more on this subject here if this is possible.

Further modifications

If you take a look at my own Table of contents page, you will notice that I have also slightly modified some other elements of the hack:

  • Changed part of the javascript to include a message above the table which explains how the contents can be used.
  • Added styling to the table to ensure it matches the styling of my blog
  • A slight change to the text above the "Show TOC" link

To add the styling for the template, I added some lines of extra CSS to the <b:skin> area of my template. Here is the CSS styling I added for your reference if you would prefer to also style your own TOC post pages:

The code I used

If you would like to use the modified javascript used here at Blogger Buster to display the TOC hack in a post page, feel free to copy the code below and paste this into your post page:

<div id="toclink"><p>On this page you can see the whole list of posts published.</p> <p>To show the full table of contents, please click on the link below (this may take a few moments to load)</p> <p><a href="javascript:showToc();"><b>Show table of contents</b></a></p> <br/><br/><br/><br/></div> <script style="text/javascript" src="http://bloggerbuster.com/scripts/blogtoc.js"></script> <script src="http://YOURBLOGURL.com/feeds/posts/default?alt=json-in-script&max-results=999&callback=loadtoc"></script> <div id="toc"></div>

Don't forget to change "YOURBLOGURL" to the proper url for your blog!

Adding a link to your Table of Contents page

You will probably want to make a link in your sidebar to the table of contents page. Just visit the page you have created and copy the url from the address bar. Then create a new text widget (or HTML/Javascript if you prefer), create your link text, and then make this text into a hyperlink using the url you have copied. Then your blog readers will easily be able to find your table of contents page and will not be troubled by long page loading times!

Technorati Tags: | | | |

Author: Amanda Kennedy

Amanda is a professional blogger and web designer living in Sheffield, United Kingdom.

In addition to curating Blogger Buster, you can find Amanda on Twitter, Facebook or add her to your circle on Google+.

Like to share?

You may also like to subscribe to Blogger Buster's RSS feed or receive free email updates of our latest posts.

30 Comments:

  1. Works great! I've been waiting for something like this in a long time. I didn't guess it was so simple. Thanks for the e-mail, Amanda. I'm using your js version. That okay? :)

    Also, I noticed you removed the inline comments. Was it because of the redirection?

    ReplyDelete
  2. No problems using my version of the javascript, I'm glad this works okay for you Angel :)

    Yes, the redirection of the inline comments was quite a problem for me. I still don't know why this was happening, and so have removed this temporarily at least until I've figured out a solution.

    ReplyDelete
  3. Hi Amanda,
    Could you please advise me what can i do to let the TOC load automatically inside the post page without having to click "Show TOC"

    manu thanks.

    Love your blog, tons of good content :)

    ReplyDelete
  4. Hello Ashley,

    Making the ToC load at the same time as the page is something I have not yet been able to do. I did try many different methods though so far nothing has worked for me (and indeed many of my attempts broke the code of the page!).

    If I ever do find a solution for this I will be sure to post about this here. In the meantime, the only way to use the ToC (as far as I am aware) is to create the clickable link. Sorry I can;t be of more help on this one!

    ReplyDelete
  5. That was awesome... thank you very much...

    ReplyDelete
  6. I have change my layout and when I click on the toc everything else on my layout goes underneath. Can you please help

    ReplyDelete
  7. It takes long time to load and user have to click show table of contents to see it . Aren't there any way to show table of contents directly to blog post without clicking any link.

    Cant this links opened in new window.

    ReplyDelete
  8. Amanda,

    You have a great blog here. It has been a great resource for me. I really like this TOC approach. Great idea.

    I figured out a way to have the TOC display automatically when you go to the TOC post. Just add "'<'script type="text/javascript"'>'showToc();'<'/script'>'" at the very end of the post. (just correct the spelling of 'script' first)

    Check out how it works here.

    Thanks for all your work.

    ReplyDelete
  9. Opps forget what I wrote in (). Instead make sure you remove the apostrophe around the < > symbols. If that makes any sense. Hope this helps a few out there.

    ReplyDelete
  10. Hi - I copied the code to a post on my blog - nothing happens when I click the "Show TOC" link. Is there some setting in my blog I need to check?
    Thanks -- ted

    ReplyDelete
  11. Never mind my last comment - I got it to work and it looks great. One question, is there a way to set the default sort by date (newest first) rather than alphabetically by post name?
    regards -- ted

    ReplyDelete
  12. http://home.planet.nl/~hansoosting/downloads/blogtoc.js

    is changed into

    http://home.kpn.nl/oosti468/downloads/blogtoc.js

    Balaji J H

    ReplyDelete
  13. http://bloggerbuster.com/scripts/

    How is this possible how did you created a directory in blogger is it possible if it is tell me how....

    Balaji J H

    ReplyDelete
  14. Hello,

    I tried your hack for TOC (hans hack modified). But it is not working. I have vista operating system on laptop. Nothing displays after i click "show TOC". Even the link shown on your page (http://www.bloggerbuster.com/2007/05/toc.html) does not show all posts. Can you help me in this regard. Also tried your modified hack
    My blog url is http://indiaspirituality.blogspot.com/
    I have labeled this post as sitemap predated 1.1.09. Thanks

    ReplyDelete
  15. Is there a way to use feedburner's feeds where you put http://YOURBLOGURL.com/feeds/posts/default?

    ReplyDelete
  16. I put this toc code in my blog.Thanks for this code.

    ReplyDelete
  17. Can i filter the feeds? like

    http://myblogsample.blogspot.com/feeds/posts/default/-/video

    ReplyDelete
  18. Great. I've already put in my blog and have success with your code. Before that, I was try the originally code from http://beautifulbeta.blogspot.com/2007/04/blogtoc-widget-released.html, but I feel unfortunately to implement that, the 'show' is now showing.

    Thanks. I follow you..

    ReplyDelete
  19. I'm feeling success with this hack than hans have.

    Thx.

    http://softbasket.blogspot.com

    ReplyDelete
  20. great tutorial but it's hard for me to understand it. do you know where i could get the easier way to do that?

    ReplyDelete
  21. This comment has been removed by the author.

    ReplyDelete
  22. I am thoroughly convinced in this said post. I am currently searching for ways in which I could enhance my knowledge in this said topic you have posted here. It does help me a lot knowing that you have shared this information here freely. I love the way the people here interact and shared their opinions too. I would love to track your future posts pertaining to the said topic we are able to read.

    ReplyDelete
  23. Thanks for taking the time to discuss this, I feel strongly about information and love learning more on this. If possible, as you gain expertise, It is extremely helpful for me. would you mind updating your blog with more information

    ReplyDelete
  24. hello amanda, i really want to put a table of content feature in my blog. but it didnt work, can you tell me why?
    i've had follow all of the instruction, please visit my blog for helping mi. -_-a

    mmasykur.blogspot.com

    ReplyDelete
  25. Hello Amanda,

    Its not working for me .... When I click show contents link, nothing happens .... Pls help ....

    ReplyDelete
  26. Thanks to share with us the information. I wish you good luck!

    ReplyDelete

Great! You've decided to leave a comment! Please bear in mind that comments are often moderated and that rel="nofollow" is in use and spammy comments will be deleted. Let's have a meaningful conversation instead. Thanks for stopping by!

Browse through the Archives

All existing posts are still available to view while I'm working on the site, albeit seen in a much simpler interface. Feel free to browse through the archives to find tutorials, templates and articles to help you build a better blog:

Blog Archive

© Blogger Buster 2010 Home | About | Contact | Hire Me