Using microformats

Microformats allow web sites to provide semantic data to the browser in order to make it possible to present summaries of the information on a page without having to know how to parse the document itself. Firefox 3 implements a global Microformats object that provides access to microformats. This object and its API make finding and reading microformats easy to do.

Loading the microformats API

The Microformats object is created using the new JavaScript script loader added to Firefox 3. To use the API, you need to first load the object:

Components.utils.import("resource://gre/modules/Microformats.js");

Once you've loaded the microformats API, you can manage microformats using the methods listed here; for information about parsing microformats, see Parsing microformats in JavaScript.

Predefined microformats

Firefox 3 provides definitions implementing several common microformats:

adr
Represents an address (such as a street or mailing address).
geo
Represents a geographical location using latitude and longitude.
hCard
Represents contact information for a person.
hCalendar
Represents a calendar appointment entry.
tag
Used to add tags to other microformats.

Methods

add()

Adds a new microformat to the microformat module.

Note: If a microformat by the specified name already exists, it is replaced by the new one.

add(name, definition);
Parameters
name
The name of the microformat to add to the microformat module.
definition
A JavaScript structure describing the microformat. See Describing microformats in JavaScript for details.

count()

Counts the number of microformats in a document that match specified criteria.

numMicroformats = Microformats.count(name, rootElement, options);
Parameters
name
The name of the microformat to count.
rootElement
Required. The DOM element at which to begin the search. If you want to search the entire document, specify content.document.
options
Optional. If provided, this is a JavaScript object that contains zero or more of the following flags:
recurseExternalFrames
If true, child frames are included in the search. The default is true.
showHidden
If true, hidden microformats are added; otherwise they're left out. The default is false.
debug
Specify true if debug mode is in use; otherwise, specify false. The default is false.
Return value

An integer value indicating the number of microformats that match the specified criteria.

debug()

Returns a string that describes a microformat object.

Note: You can simply call debug() on a microformat object: microformatObject.debug() instead of using this method if you prefer.

dumpString = debug(microformatObject)
Parameters
microformatObject
The microformat object to dump.
Return value

A string that describes the contents of the specified microformat object.

get()

Returns an array of microformat objects corresponding to the microformats found that match specified criteria.

microformatsArray = Microformats.get(name, rootElement, options, targetArray);
Parameters
name
The name of the microformat to find.
rootElement
Required. The DOM element at which to begin the search. If you want to search the entire document, specify content.document.
options
Optional. If provided, this is a JavaScript object that contains zero or more of the following flags:
recurseExternalFrames
If true, child frames that reference external content are included in the search. The default is true.
showHidden
If true, hidden microformats are added; otherwise they're left out. The default is false.
debug
Specify true if debug mode is in use; otherwise, specify false. The default is false.
targetArray
Optional. If provided, this is an array of microformat objects to which the search results are added.
Return value

A new array of microformat objects matching the search criteria, or the array specified by microformats with the newly found microformat objects added.

getNamesFromNode()

Returns a space-delineated list of microformat names that correspond to the specified microformat node.

nameList = Microformats.getNamesFromNode(node);
Parameters
node
The node for which to retrieve a list of microformat names.
Return value

If the specified node is a microformat, the result is a space-delineated string listing all the microformat names that correspond to the node. If the node isn't a microformat, nothing is returned.

getParent()

Returns the parent node of the specified microformat or child of a microformat.

parentNode = Microformats.getParent(node);
Parameters
node
The node whose parent you wish to retrieve.
Return value

The parent of the specified node. Returns nothing if the specified node isn't a microformat or the child of a microformat.

isMicroformat()

Determines whether or not the specified DOM node is a microformat.

flag = Microformats.isMicroformat(node);
Parameters
node
The DOM node to check to see if it's a microformat.
Return value

true if the node is a microformat, otherwise false.

Note: This method does not return true if the node is a child of a microformat.

See also

Document Tags and Contributors

Contributors to this page: Sheppy, tw2113, MKaply, Suguni, sebmozilla, ethertank, teoli, Minh Nguyen, Kohei, Mgjbot
Last updated by: teoli,