Prototype JS Library

Prototype is a JavaScript framework that aims to ease development of dynamic web applications. JavaScript frameworks, such as Prototype, jQuery and MooTools have taken much of the pain of writing javascript away. 

“Prototype takes the complexity out of client-side web programming. Built to solve real-world problems, it adds useful extensions to the browser scripting environment and provides elegant APIs around the clumsy interfaces of Ajax and the Document Object Model.”, according to . 

To start working with prototype, a developer needs to have some basic knowledge about – HTML,CSS & Basic Javascript and has to put a copy of prototype.js file on his page header:

<script src="prototype.js"></script> 

Lets discuss some features of prototype –

  • Finding Elements
  • Filtering
  • Observe Events
  • Ajax Support

 Dummy HTML content for further discussion –

<span class="test" id="spanId">

span content


Finding the Elements

$("spanId").toggleClassName("highlight");                                                —> Find by ID

$$(".test").each(function(elmt) { elmt.toggleClassName("highlight") }); —> Find by Class 

$$("span").each(function(elmt) { elmt.toggleClassName("highlight") }); —> Find by Tag

“$” and “$$” are prototype functions used for different cases.

You may notice that I have also introduced some new pieces of Prototype,

the /each/ function, /toggleClassName/ function. 


$$("span:first-child").each(function(elmt) { --> Filter for the First Object of Many



$$("span:not(#second)").each(function(spanobj) {–> Filter out a Specific ID




Observe Events

Handling JavaScript objects with Prototype is easy. Any differences among browsers is automatically handled. In Prototype, you /observe/ an object, waiting for its events.

Example – The dom:ready Event

Prototype adds a special event called /dom:ready/. This can be used to know when the page should be initialised. The dom:ready event happens when the document object model (DOM) is ready to be accessed. If you put all JavaScript code, that happens on page loads, inside the dom:ready event, then you ensure that it will be run as early as possible, but not too early.

To add the dom:ready event, put this code in the JavaScript section of your page:

document.observe('dom:loaded', function() {

alert('DOM is loaded!');


Prototype Support for AJAX

All major browsers support a version of the XMLHttpRequest object that makes Ajax possible, either as an ActiveX component or as a native JavaScript object. XMLHttpRequest, however, exposes the HTTP protocol at a very low level, which gives the developer a lot of power, but also requires her to write a lot of code in order to do simple things.

Prototype uses it’s own object inheritance system to provide a hierarchy of Ajax helper objects. Ajax functionality is contained in the global Ajax object. This object provides all the necessary methods to handle AJAX requests and responses in an easy way:

new Ajax.Request('/some_url',
 onSuccess: function(transport){
   var response = transport.responseText || "no response text";
   alert("Success! \n\n" + response);
 onFailure: function(){ alert('Some problem occurred...') }

 Other features 

Other than these features, prototype.js also has support for Number processing, Array processing, JSON support, Templating, Enumerating etc. 

Comparision b/w Jquery and Prototype 

Comparing these two frameworks is indeed a very difficult task to do. After studying and searching whatever information is available on web, I can say that it depends on what environment the individual feels more comfortable. The following blogs solely focus on this question – 

Just like Jquery UI tools, prototype also has its UI extensions like –




Comments are closed.