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
span elements with the footnotes class, and blockquote elements with a title attributewithin the outer selection (e.g., within any div elements, in this case). The matches will be extracted and moved to footnotes.
Here is a simple sentence, with a footnote at the end (This is the footnote.).
Here is a simple blockquote, with a title.
<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>
There are a number of options that can be provided by passing the appropriate values to the footnotes() method.
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.
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.
footnotes() method is invoked will have its own footnote list. This is the default.Specifies the ID (without the leading #) where footnotes will be placed.
Allows for the selection of a block within the outer block, to which the ordered list where footnotes will be placed will be appended.
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.)
Function to extract the footnote HTML from the elements that are selected by the footnotes option.
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.
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.)id attribute to footnote link, so links back work in Internet Explorer. (Thanks to an anonymous commenter for this suggestion.)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!
Post new comment