Prototype JavaScript Framework
From Wikipedia, the free encyclopedia
Prototype JavaScript Framework | |
Developer: | Sam Stephenson |
---|---|
Latest release: | 1.5.0 / January 18, 2007 |
Use: | JavaScript toolkit |
License: | MIT License |
Website: | prototypejs.org/ |
The Prototype JavaScript Framework is a JavaScript framework that provides an Ajax framework and other utilities. Though available as a standalone library, Ruby on Rails integrates the framework as well as other projects such as script.aculo.us and Rico.
Contents |
[edit] Features
Prototype provides various functions for developing JavaScript applications. The features range from programming shortcuts to major functions for dealing with XMLHttpRequest.
[edit] Sample utility functions
[edit] The $() function
To refer to an element in the DOM of an HTML page, the usual function identifying an element is:
document.getElementById('name_of_id');
The $() function reduces the code to:
$('name_of_id');
This function can be used as the getElementById() function. For example, you can set the CSS text color with this code:
$('name_of_id').style.color = "#ffffff";
[edit] The $F() function
Building on the $() function: 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.
$F('name_of_id');
- Note: Like the underscore
_
, the$
character is a legal "word character" in JavaScript identifiers, and has no other significance in the language. It was added to the language at the same time as support for regular expressions, so that the Perl-like matching variables could be emulated, such as$`
and$'
.
[edit] The Ajax object
In an effort to reduce the amount of code needed to run a cross-browser XMLHttpRequest function, the Ajax object offers an easier way to invoke the function without the need to code with specific browsers in mind. There are two forms of the Ajax Object. Ajax.Request returns the raw XML output from an AJAX call, while the Ajax.Updater will inject the return inside a specified DOM object.
The Ajax.Request below finds the values of two HTML value inputs, requests a page on the server using the values as POST values, then runs a custom function called showResponse() when complete:
var value1 = $F('name_of_id_1'); var value2 = $F('name_of_id_2'); var url = 'http://yourserver/path/server_script'; var pars = 'value1=' + value1 + '&value2=' + value2; var myAjax = new Ajax.Request( url, { method: 'post', parameters: pars, onComplete: showResponse });
[edit] Object-oriented programming
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 prototype
prototype which acts as a blueprint for instances of the class. Finally, old classes can be extended by new classes using Object.extend
.
var FirstClass = Class.create(); FirstClass.prototype = { // The initialize method serves as a constructor initialize:function() { this.data = "Hello World"; } }; var DataWriter = Class.create(); DataWriter.prototype = { printData:function() { document.write(this.data); } } Object.extend(DataWriter, FirstClass);
[edit] Footnotes
[edit] Related links
[edit] External links
- Prototype Javascript Framework home page
- Official Prototype API documentation
- Official Prototype API documentation (PDF book)
- Sergio Pereira's Prototype API documentation
- Prototype Window Library
- Rico - A Prototype Based Library
- Working With Events In Prototype
- Prototype JavaScript Framework documentation wiki with examples