Far above any other issue, event handling is
the biggest problem that web developers using JavaScript have to deal with. Libraries solve
this problem by creating a unified interface for attaching events, maintaining object scope,
and stopping events. Let??™s take a look at an example from Prototype:
Event.observe(element, 'click',
(function(){ alert(this.href) }).bindAsEventListener(element)
);
In Prototype an Event object has an observe() method that enables you to observe
events on a particular object. You want to track a click event on an element (a link, in this
case). The third parameter enables you to pass in a function to be called when the event is
fired. Because this is a simple example, I created an anonymous function, but notice the
method bindAsEventListener(). This method takes a single parameter: the element that
should have scope from within the function. When the function gets called, this will refer
to element. The bindAsEventListener() method makes use of the apply() method (refer to
Chapter 3), which ensures that scope is maintained.
Ajax
Ajax originally stood for Asynchronous JavaScript and XML, but it has morphed into an
umbrella term that encapsulates a number of technologies. At the core of Ajax, though, is still
the idea of using JavaScript to communicate with the server to send and receive data without
having to refresh the page. This is done using the XMLHttpRequest object, often referred to as
the XHR object.
Pages:
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143