SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 225 | Next

Jonathan Snook, Aaron Gustafson, Stuart Langridge, and Dan Webb

"Accelerated DOM Scripting with Ajax, APIs, and Libraries"


2. The onBlur handler, which runs when the user??™s focus leaves a field, should grab the
value out of the field and make an XMLHttpRequest call to the server, passing the field??™s
name and field value.
3. The server then runs the validation code??”this is exactly the same validation code that
the form uses on the server side, not any kind of copy or duplicate of it??”and returns
the validate() function??™s result.
4. The JavaScript receives the result of the Ajax call, and if it??™s an error message, it updates
the page with the error.
Again, it??™s important to note that there??™s only one validate() function. The server-side
code calls it when doing validation of the form, and the enhanced form calls the same function
via Ajax.
There are a number of parts to building this new form. The previous regularexpressions-
based example didn??™t do anything hugely complicated in terms of JavaScript.
With an Ajax-based approach you need to attach events, make an Ajax call, parse the
results of the Ajax call into something meaningful, and update the DOM of the page. This
is the ideal time to start thinking about introducing a JavaScript library to do some of the
heavy lifting. All the libraries out there will make DOM manipulation and Ajax calls simple,
and having a library do XMLHttpRequest is far easier than handling the complexities
of it yourself in most cases.
For this example you??™ll use jQuery, which is very easy if the application you??™re building is
on the Internet: simply add the following to the of your page: