Prototype JavaScript Framework

From Wikipedia, the free encyclopedia

(Redirected from Prototype.js)
Jump to: navigation, search
Prototype JavaScript Framework
Developer Prototype Core Team
Latest release 1.6 / Nov 7, 2007
Genre JavaScript toolkit
License MIT License
Website prototypejs.org/

The Prototype JavaScript Framework is a JavaScript framework created by Sam Stephenson 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

Prototype provides various functions for developing JavaScript applications. The features range from programming shortcuts to major functions for dealing with XMLHttpRequest.

Prototype adds a new method for creating classes and extending existing classes. This is made at the library level.

To refer to an element in the DOM of an HTML page, the usual function identifying an element is:

document.getElementById("id_of_element")

The $() function reduces the code to:

$("id_of_element")

This function can be used as the getElementById() function. For example, you can set the CSS text color with this code:

$("id_of_element").style.color = "#ffffff";

Or, the "Prototype way":

$("id_of_element").setStyle({color: '#ffffff'});

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("id_of_input_element")
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 $'.

Building on the $() function: the $$() function returns all the elements that match, ie if you wanted all the elements (all the links) you would request them by the following

$$("a")
Note: returns a list, from there you can do
$$("a").each(Effect.Pulsate);

This will pulsate all the links - similar to blink. Note: Effect.Pulsate is part of Script.aculo.us, not Prototype.

In an effort to reduce the amount of code needed to run a cross-browser XMLHttpRequest function, Prototype provides the Ajax object to abstract the different browsers. It has two main methods: Ajax.Request() and Ajax.Updater(). 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 from 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=" + encodeURIComponent(value1) + "&value2=" + encodeURIComponent(value2);
 
var myAjax = new Ajax.Request(
   url,
       {
                method: "post",
                parameters: pars,
                onComplete: showResponse
       });

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);

Extending another class:

Ajax.Request.prototype = Object.extend(new Ajax.Base(), { 
  initialize: function(url, options) { 
    this.transport = Ajax.getTransport(); 
    this.setOptions(options); 
    this.request(url); 
  }, 
  // ...snip ... 
}

Notice the comma at the end before the snip. Since javascript does not have language features for doing extend. Prototype has a method called 'extend' that has two parameters.



Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.