AJAX Prototype
Prototype framework enables you to deal with Ajax calls in a very easy and fun way that is also safe (cross-browser). Besides simple requests, this module also deals in a smart way with JavaScript code returned from a server and provides helper classes for polling.
The biggest part of the Prototype framework are its DOM extensions. Prototype adds many convenience methods to elements returned by the $() function: for instance, you can write $(’comments’).addClassName(’active’).show() to get the element with the ID ‘comments’, add a class name to it and show it (if it was previously hidden). The ‘comments’ element didn’t have those methods in native JavaScript; how is this possible? This document reveals some clever hacks found in Prototype.
Using the $() function
The $() function is a handy shortcut to the all-too-frequent document.getElementById() function of the DOM. Like the DOM function, this one returns the element that has the id passed as an argument.
function test(){
var d = $(’myDiv’);
alert(d.innerHTML);
d.hide();
d.show();
d.addClassName(’active’);
}
Using the $F() function
The $F() function is another welcome shortcut. It returns the value of any field input control, like text boxes or drop-down lists. The function can take as argument either the element id or the element object itself.
function test3()
{
alert( $F(’userName’) );
}
