Using microformats
From MDC
This article covers features introduced in Firefox 3
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.
Contents |
[edit] 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.
[edit] 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.
[edit] Methods
[edit] add()
Adds a new microformat to the microformat module.
add(name, definition);
[edit] 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.
[edit] count()
Counts the number of microformats in a document that match specified criteria.
numMicroformats = Microformats.count(name, rootElement, options);
[edit] 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 istrue.- showHidden
- If
true, hidden microformats are added; otherwise they're left out. The default isfalse.- debug
- Specify
trueif debug mode is in use; otherwise, specifyfalse. The default isfalse.
[edit] Return value
An integer value indicating the number of microformats that match the specified criteria.
[edit] debug()
Returns a string that describes a microformat object.
debug() on a microformat object: microformatObject.debug() instead of using this method if you prefer.dumpString = debug(microformatObject)
[edit] Parameters
- microformatObject
- The microformat object to dump.
[edit] Return value
A string that describes the contents of the specified microformat object.
[edit] get()
Returns an array of microformat objects corresponding to the microformats found that match specified criteria.
microformatsArray = Microformats.get(name, rootElement, options, targetArray);
[edit] 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 istrue.- showHidden
- If
true, hidden microformats are added; otherwise they're left out. The default isfalse.- debug
- Specify
trueif debug mode is in use; otherwise, specifyfalse. The default isfalse.
- targetArray
- Optional. If provided, this is an array of microformat objects to which the search results are added.
[edit] 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.
[edit] getNamesFromNode()
Returns a space-delineated list of microformat names that correspond to the specified microformat node.
nameList = Microformats.getNamesFromNode(node);
[edit] Parameters
- node
- The node for which to retrieve a list of microformat names.
[edit] 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.
[edit] getParent()
Returns the parent node of the specified microformat or child of a microformat.
parentNode = Microformats.getParent(node);
[edit] Parameters
- node
- The node whose parent you wish to retrieve.
[edit] Return value
The parent of the specified node. Returns nothing if the specified node isn't a microformat or the child of a microformat.
[edit] isMicroformat()
Determines whether or not the specified DOM node is a microformat.
flag = Microformats.isMicroformat(node);
[edit] Parameters
- node
- The DOM node to check to see if it's a microformat.
[edit] Return value
true if the node is a microformat, otherwise false.
true if the node is a child of a microformat.