Example 2: Page Glossary

This script is pageGlossary from Easy Designs (url="easy-designs.net/code/pageGlossary/"/>). It traverses a specified portion of the document (identified by an id), collecting all of the abbreviation, acronym, and definition elements, and writes them out to the designated portion of the page (also identified by an id) as a formal page glossary. The script also removes duplicate entries and sorts the contents alphabetically. Here it is:

var pageGlossary = { getFrom: false, // where to collect terms from buildIn: false, // where to place the glossary glossArr: [], // the working glossary as an array usedArr: [], // terms we've used ( to track duplicates ) init: function( fromId, toId ){ /* init( ) takes two arguments: * id of the collection area * id of the destination */ // make sure the required methods and elements are available if( !document.getElementById || !document.getElementsByTagName || !document.getElementById( fromId ) || !document.getElementById( toId ) ) return; // set the collection area pageGlossary.getFrom = document.getElementById( fromId ); // set the destination area pageGlossary.buildIn = document.getElementById( toId ); // run the collection method (below) pageGlossary.collect( ); // if the glossary array has no members, quit now if( pageGlossary.usedArr.length < 1 ) return; // resort the array in alphabetical order using ksort (below) pageGlossary.glossArr = pageGlossary.ksort( pageGlossary.glossArr ); // run the build method (below) ...

Get Web Design in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.