Ajax: It's not only code

Ajax works outside the page but safely inside the JavaScript sandbox.

1 2 3 4 5 6 Page 3
Page 3 of 6

Other than the whizbang factor, Ajax doesn’t add much to this type of functionality. However, Ajax is terrific when it comes to validating a login or other form content because you don’t lose what you’ve already typed in.

As for using Ajax to create applications to replace word editors, I already have a terrific editor: NeoOffice, the Mac frontend to OpenOffice. I don’t need a browser-based alternative; the huge majority of people don’t. However, when I use my online weblog-editing tool, I like some of the Ajax features; for example, I can pull up categories only when I click a toolbar, and thus select a category other than the default.

In other words, Ajax is a tool. It is not a mindset, philosophy, or badge of coolness. Definitely use it, but only when it makes sense. As Star Trek’s Scotty would say, “How many times do I have to say it? Use the right tool for the job.”

Beam me up, Scotty.

How Ajax Works

Ajax is not as complicated as it may seem at first. A request needs to be sent to the server, a service invoked, and data returned. However, instead of submitting a form and loading a new page with the response, Ajax handles all of this activity within the context of the same page.

A special object, either Microsoft’s ActiveXObject or the more general XMLHttpRequest, manages the asynchronous communication between the server and the client. Asynchronous means that the request is sent, but the client doesn’t have to stop, hold, and wait for the process to finish; there is no twirly icon to signal working while you twiddle your thumbs. Instead, the client provides a function to be called when the state of the request changes. In this function, this state is checked; then, based on its value, as well as the status of the request, the data returned from the service is processed and usually output to the page in some form.

To the web-page reader, all of this activity looks as if the processing is happening within the page, rather than through client/server interaction. The only indicator that server access is happening is if this information is specifically provided.

Tip

Ajax does require a server-side component. I’m using PHP for this book because PHP is probably one of the most common scripting languages used today. Also, in my opinion, of all the server-side scripting languages available—Perl, Python, Ruby, and PHP—I consider PHP to be the most JavaScript-like.
1 2 3 4 5 6 Page 3
Page 3 of 6
  
Shop Tech Products at Amazon