From Wikipedia, the free encyclopedia
|Developer(s)||Prototype Core Team|
|Stable release||1.7.1 / August 8, 2012|
Sample utility functions
The $() function
The dollar function, $(), can be used as shorthand for the getElementById function. To refer to an element in the Document Object Model (DOM) of an HTML page, the usual function identifying an element is:
The $() function reduces the code to:
The $() function can also receive an element as parameter and will return, as in the previous example, a prototype extended object.
- Note: Like the underscore (
The $F() function
Building on the
$F()function returns the value of the requested form element. For a 'text' input, the function will return the data contained in the element. For a 'select' input element, the function will return the currently selected value.
The $$() function
The dollar dollar function is Prototype's CSS Selector Engine. It returns all matching elements, following the same rules as a selector in a CSS stylesheet. For example, if you want to get all
elements with the class "pulsate", you would use the following:
This returns a collection of elements. If you are using the script.aculo.us extension of the core Prototype library, you can apply the "pulsate" (blink) effect as follows:
The Ajax object
In an effort to reduce the amount of code needed to run a cross-browser
XMLHttpRequestfunction, Prototype provides the
Ajaxobject to abstract the different browsers. It has two main methods:
Ajax.Updater(). There are two forms of the
Ajax.Requestreturns the raw XML output from an AJAX call, while the
Ajax.Updaterwill inject the return inside a specified DOM object. The
Ajax.Requestbelow finds the current values of two HTML form input elements, issues an HTTP POST request to the server with those element name/value pairs, and runs a custom function (called
showResponsebelow) when the HTTP response is received from the server:
Prototype also adds support for more traditional object-oriented programming. The
Class.create()method is used to create a new class. A class is then assigned a
prototypewhich acts as a blueprint for instances of the class.
Extending another class:
The framework function
Object.extend(dest, src)takes two objects as parameters and copies the properties of the second object to the first one simulating inheritance. The combined object is also returned as a result from the function. As in the example above, the first parameter usually creates the base object, while the second is an anonymous object used solely for defining additional properties. The entire sub-class declaration happens within the parentheses of the function call.