JasonWyatt

This blog is full of stuff, and things.

Find my resume.

permalink

Footnotes.js - Automatic Footnote Creator

This is another project I worked on a few months ago. It’s a script that will automatically scan your document for footnote anchors and generate links to the footnotes you specify in an ordered list anywhere on the page. It requires jQuery to operate.

Tutorial

  1. Download the zip and extract it into a folder within your site somewhere. For the sake of this tutorial we’ll say you unzipped it into the directory /httpdocs/footnotes, where /httpdocs is the root of your web site and is where the HTML file we’re going to use footnotes with resides.
  2. In your HTML document’s <head> section add the following lines:

    <link ref="stylesheet" href="./footnotes/footnotes.css" type="text/css" charset="UTF-8" />
    <script type="text/javascript" src="./footnotes/footnotes.js" charset="UTF-8"></script>
  3. There are two main areas to think about when you want to reference a footnote:

    • the text that is referring to the footnote,
    • and the footnote itself.

    With footnotes.js, you refer to a footnote by using an anchor tag and you store your footnotes in a list.

    Depending on the settings you specify within footnotes.js, what you need to do can vary, but we’ll assume you stuck with the default values.

    Assume you have the following paragraph in your HTML (from Jules Verne’s Around the World in Eighty Days):

    <p>
      Mr. Phileas Fogg lived, in 1872, at No.7, Saville Row, Burlington Gardens, the house in which Sheridan died in 1814. He was one of the most noticeable members of the Reform Club, though he seemed always to avoid attracting attention; an enigmatical personage, about whom little was known, except that he was a polished man of the world.  People said that he resembled Byron - at least that his head was Byronic; but he was a bearded, tranquil Byron, who might live on a thousand years without growing old.
    </p>

    And assume we want to add a link to a footnote after the first sentence that says this:

    At least we think it was the house where Sheridan died, but we're not 100% sure.

    As well as another footnote referenced after each mention of Byron that has a link to the Wikipedia article about Byron:

    See the article at <a href="http://en.wikipedia.org/wiki/George_Gordon_Byron,_6th_Baron_Byron">Wikipedia</a> to learn who George Gordon Byron was.

    We would introduce three anchor tags into the paragraph which would look like so:

    <p>
      Mr. Phileas Fogg lived, in 1872, at No.7, Saville Row, Burlington Gardens, the house in which Sheridan died in 1814.<a class="footnote"></a> He was one of the most noticeable members of the Reform Club, though he seemed always to avoid attracting attention; an enigmatical personage, about whom little was known, except that he was a polished man of the world.  People said that he resembled Byron<a class="footnote"></a> - at least that his head was Byronic; but he was a bearded, tranquil Byron<a class="footnote" name="2"></a>, who might live on a thousand years without growing old.
    </p>

    If you notice, you’ll see two different kinds of anchor tags we added there:

    • One with only a class attribute “footnote” - These will get auto-numbered by the script in the order in which they appear. This is useful if you know your references will be in the same order as the footnote list you’re using.
    • One with two attributes: class and name - The class attribute is still set to “footnote”, but the name attribute is identifying the “target” footnote to reference (I would’ve loved to make the attribute be the “target” HTML attribute, but in XHTML Strict there is no such thing).

    Of course we also need to add a list containing the content of our actual footnotes (you can put it anywhere):

    <ol id="footnotes">
      <li class="footnote">
        At least we think it was the house where Sheridan died, but we're not 100% 
        sure.
      </li>
    
      <li class="footnote">
        See the article at 
        <a href="http://en.wikipedia.org/wiki/George_Gordon_Byron,_6th_Baron_Byron">Wikipedia</a>
        to learn who George Gordon Byron was.
      </li>
    <ol>
  4. There we go, you’re done!

permalink

toc.js - Table of Contents Injector

toc.js will scour your page for heading tags and build a table of contents for you. All you have to do is create an empty ordered list and give it a specific ID that the script will look for. toc.js will then automatically construct the table of contents and set up anchor-links for your users to use to quickly navigate to specific sections of your page.

Download it!

You can download toc.js from here. (Contains toc.css and a readme file in addition to the JavaScript)

I should probably turn this into a proper jQuery plugin eventually, but I wanted to get it out there.