AJAX - The Complete Reference

(avery) #1

222 Part II: Applied Ajax^


look carefully at the examples presented in the table, you can see the reason for the major
criticism that people have with Prototype: it has extended or even overridden core
JavaScript features. For example, the stripTags() method has been added as a prototype
to all strings in JavaScript. This is certainly helpful and explains greatly why the library is
named the way it is. However, there is a dark side here. What happens if another script or
library in the page expects JavaScript types or methods to act in their default manner?
Obviously, there is great potential for conflict.

NNOT EOTE Critics of Prototype are quite vocal in pointing out that the library often does not play well
with others. Most often such conflicts arise due to the overriding and extension of various core
JavaScript facilities. However, in some cases it is simply the fact that everyone wants to use the
$() function in a different way.

Function Description Example
escapeHTML (string) Converts XHTML tags
into escaped character
entities.

var tab = "<table><tr><td>TEST</
td></tr></table>";
var tabesc = tab.escapeHTML();
clear (array) Removes all items from
an array.

arr.clear();

evalJSON (string) Evaluates a JSON string
and returns the object.
Accepts an optional
sanitize parameter that
won’t do the eval()
if it finds any malicious
content.

var obj = "{name:Alex, age:2}".
evalJSON(true);

first (array) Returns the first element
of an array.

var firstItem = arr.first();

indexOf (array) Returns the index of item
the val passed into the
functions. Similar to the
indexOf() for strings.

var index = arr.indexOf("Giants");

parseQuery (string) Splits a quer y string
into an associative array
indexed by parameter
name.

var query = "a=123&b=456&c=789";
var queryObj = query.parseQuery();

stripTags (string) Returns the string with
all HTML tags removed.

var tab = "<table><tr><td>TEST</
td></tr></table>";
var tabesc = tab.stripTags();
without (array) Returns the array without
the values indicated.

var cleanArray = oldArray
.without("Dodgers", "Padres");

TABLE 5-10 More Prototype Utilities
Free download pdf