jQuery Footnotes

bholt's picture

jFootnotes is a jQuery plugin that creates and formats footnotes based on inline content. I have used it fairly extensively on this site, and thought I’d share it with the world. It has been tested with jQuery 1.2.6, but I have no reason to believe it won’t work with 1.3.x as well.

Once the module has been included in a page, it can be called as follows:

$("div").footnotes();

Pretty straightforward. By default, it will look for

  • any span elements with the footnotes class, and
  • any blockquote elements with a title attribute

within the outer selection (e.g., within any div elements, in this case). The matches will be extracted and moved to footnotes.

Examples

Here is a simple sentence, with a footnote at the end (This is the footnote.).

Here is a simple blockquote, with a title.

    Source

    <div class="example"> <p> Here is a simple sentence, with a footnote at the end <span class="footnote"> (This is the footnote.)</span>. </p> <blockquote title="I came from the blockquote."> <p>Here is a simple blockquote, with a title.</p> </blockquote> <ol id="autoFootnotes0" class="footnotesList"></ol> </div> <script type="text/javascript"> <!-- $(document).ready(function() { $(".example").footnotes(); }); // --> </script>

    Options

    There are a number of options that can be provided by passing the appropriate values to the footnotes() method.

    footnotes

    Overriding the footnotes option allows users to select which entities get turned into footnotes. Note that the fnExtractFootnote option should be probably be overridden as well.

    prependTags

    By default, the link to the footnote will be appended to the end of an element selected by the footnotes selector. If some elements should have their footnote links prepended, appropriate selectors should be added here. Note that elements selected here are a subset of those selected by footnotes.

    For example, if automatic quotation marks are added to p elements within blockquotes, one might use the :last-child pseudo-selector to trigger the addition of close quotes to the last paragraph. However, if a sup tag is appended to the blockquote, :last-child will no longer match the last p, and the quotes will not be added. Adding blockquote here will cause the sup tag to be prepended, avoiding this issue.

    By default, the value of this option is FALSE.

    singleFootnoteDestination

    • When TRUE, all footnotes will be put into a single ordered list.
    • When FALSE, each element on which the footnotes() method is invoked will have its own footnote list. This is the default.

    destination

    Specifies the ID (without the leading #) where footnotes will be placed.

    contentBlock

    Allows for the selection of a block within the outer block, to which the ordered list where footnotes will be placed will be appended.

    autoFootnoteClass

    CSS class name to be added to each of the elements matching the footnotes selector option. This allows us to ensure that the footnotes are added to the list in the correct order. (Set this to something that won't be used otherwise, because it is what will eventually be used to select all the elements to be turned into footnotes.)

    fnExtractFootnote

    Function to extract the footnote HTML from the elements that are selected by the footnotes option.

    orderedList

    • When TRUE, footnotes will be added to an ordered list. This is the default.
    • When FALSE, footnotes will be added to an unordered list.

    debugMode

    • When TRUE, we will attempt to output console log messages.
    • When FALSE, console log messages will be suppressed. This is the default.

    Downloads

    The plugin can be downloaded in an archive containing both the full and minified versions of the plugin. This distribution is in the form of a Drupal module, allowing one to easily integrate it into that content management system, but it should be fairly straightforward to use just the JavaScript portions.

    Updates

    Version 1.0
    Initial release.
    Version 1.1
    Internal version containing some assorted bug fixes.
    Version 1.2—2009-04-05
    • Contains fixes explicitly marking the footnote links as being left-to-right content. This allows footnotes to be placed after content in a right-to-left language (e.g., Arabic) without display errors. See this blog post for an example where this is used.
    • Tweaks the default CSS formatting.
    • Tweaks the regular expression to extract the text of inline footnotes to work when the opening whitespace contains non-breaking spaces.
    • Fixes an issue in IE where the footnote text was not appearing, and the numbering was wrong. Footnote numbering will not appear in IE in this version.  (This is a CSS issue, so others using different styling to the default I’ve included may not have the problem.)
    Version 1.2.2—2010-01-18
    • Fixed bug that caused footnote links to have the wrong numbers when multiple identical footnotes are used on a page. (Thanks to Sergey Kovalev for reporting this bug.)
    • Added title attribute to footnote link, so users can hover on the link to read the footnote text. (Thanks to an anonymous commenter for this suggestion.)
    • Added id attribute to footnote link, so links back work in Internet Explorer. (Thanks to an anonymous commenter for this suggestion.)

    Licensing

    This code is copyrighted 2008–2010 by Brian Holt, and licensed for distribution under version 3.0 of the GNU Lesser General Public License.

    Thanks to CSSNewbie.com for the idea, which I have extended and turned into an actual plugin.

    Comments

    Excellently written article, if only all bloggers offered the same content as you, the internet would be a much better place. Please keep it up! Cheers.

    @Zoran—thanks for the compliment! I do my best. :-)

    Many thanks

    this is such a great plugin. i need this for college.

    Great product!
    My feedback is simply:
    1. You'll need to add: .attr("id", "cite-ref-" + refID) to this section of the js
    // First, add the link to the footnote
    // Add a new A tag and set its properties
    $anchor = $("")
    .attr("href", "#cite-text-" + refID)
    .attr("name", "cite-ref-" + refID)
    .attr("id", "cite-ref-" + refID)
    .attr("dir", "ltr")
    .text("[Footnote " + (i + 1) + "]")
    .addClass("footnoteLink");

    otherwise IE6 doesn't have an ID to link back up to...worked OK without this extra line in Firefox but won't work in IE6 unless it's there.

    2. Any chance of adding in a tooltip like effect where the info appears when hovering on the footnote link? That way you don't have to click to the bottom (to read the footnote).

    3. From an accessibility standpoint, the footnote link can be changed from .text("[" + (i + 1) + "]") to .text("[Footnote " + (i + 1) + "]") and the corresponding ^ link can be changed to .text("Return to source paragraph: footnote " + (i+1)). This way each link is unique and fully descriptive and is therefore accessible to visually impaired users (JAWS software users etc), plus the link target is big enough for users with dexterity issues.

    Anyhow, just some ideas to mull over. Again, great product!

    I have uploaded version 1.2.2 which, among other things, incorporates your suggestions 1 and 2. While I see your point in #3, I don’t think the larger links are as aesthetically pleasing, and so I have not made any changes in how they are presented. Thanks for your suggestions and encouragement!

    Have an issue when using IE8 and adding footnotes using the span tag.
    This does not show the mouseover tooltip or give you a hyperlink to the bottom of the page where the footnotes are located. It does write the [1], just as sup text.

    Putting IE8 in compatibility mode makes it work, but is not a production solution.

    Firefox and Safari have not shown this issue. Seems to be just IE8.

    Any ideas?

    Hmm.  I don't have access to IE8 at the moment, but I'll try to get Windows installed in a VM and see if I can check it out.

    Post new comment

    The content of this field is kept private and will not be shown publicly.
    • Web page addresses and e-mail addresses turn into links automatically.
    • Allowed HTML tags: <a> <abbr> <acronym> <em> <blockquote> <hr /> <img /> <strike> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <div> <span> <sup> <sub> <h3> <h4> <h5> <h6>
    • Lines and paragraphs break automatically.
    • You may link to Gallery2 items on this site using a special syntax.
    • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.
    • You may quote other posts using [quote] tags.
    • You may insert videos with [video:URL]

    More information about formatting options

    CAPTCHA
    This question is for testing whether you are a human visitor and to prevent automated spam submissions.