Current

This document is valid for the current version of Plone.

Recommendations for JavaScript Components

by Plone Documentation Team last modified Feb 26, 2011 09:13 PM
Contributors: Mikko Ohtamma, Martin Aspeli, Kamon Ayeva, Israel Saeta Pérez
JavaScript components should follow the principles of progressive enhancement and unobtrusive JavaScript.

Progressive Enhancement

Pages presented in Plone, including forms and form widgets, must be fully usable in situations where JavaScript is not available. The availability of JavaScript on the browser side should enhance the presentation of the page and its content. Additionally, the structured document delivered via HTML/XHTML should be semantically correct and complete in meaning.
The best way to accomplish these goals is to: first. compose complete and useful content in HTML; second, style its presentation with CSS; and third, make use of JavaScript behaviors to enhance presentation and interaction.

Unobtrusive JavaScript

JavaScript should nearly never be present in the content area of a page. Typically, it will only appear via link and script elements in the head of the document (or at its very end when that improved rendering).
In particular, HTML tags should nearly never have event-handler (e.g., onclick or onsubmit) tag attributes or JavaScript in URLs. Coding JavaScript into HTML tags generally makes for code that is hard to maintain and nearly impossible to test.
Instead of coding event handlers in HTML attributes, use jQuery’s “bind” and its various convenience aliases like “click” methods to attach event handlers to elements. Use “live” if installing behaviors that need to operate in AJAX-loaded HTML.