Home arrow JavaScript arrow Page 3 - Working with AJAX and the Prototype JavaScript Library
JAVASCRIPT

Working with AJAX and the Prototype JavaScript Library


In the last few months, the Prototype JavaScript library has become very popular with web developers. If you want to take your first steps with it, you should begin reading this article. Welcome to the second part of the series “Introducing the Prototype JavaScript library.” This series shows you how to put this powerful package to work for you to facilitate the development of your own JavaScript applications.

Author Info:
By: Alejandro Gervasio
Rating: 4 stars4 stars4 stars4 stars4 stars / 3
February 06, 2007
TABLE OF CONTENTS:
  1. · Working with AJAX and the Prototype JavaScript Library
  2. · Using the Prototype/AJAX combination
  3. · Dealing with formatted (X)HTML results
  4. · Using the Try.these() function and the each loop

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Working with AJAX and the Prototype JavaScript Library - Dealing with formatted (X)HTML results
(Page 3 of 4 )

As I stated earlier, the Prototype library will let you deal easily with server results that are formatted as (X)HTML. This process can be quickly accomplished by using the handy "AJAX.Updater" class, which can be used in the following way:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-
8859-1" />
<title>Example Ajax.Updater class</title>
<script language="javascript" src="prototype-1.4.0.js"></script>
<script language="javascript">
// example using the Ajax.Updater class
function fetchHTML(){
   var ajaxobj= new Ajax.Updater
('datacontainer','gethtmldata.php',{method: 'get', parameters:
'a=1&b=2',onFailure: displayError});
}
function displayError(requestObj){
   $('datacontainer').innerHTML='Failed to receive server
response!';
}
window.onload=function(){
   if(document.getElementById && document.createElement &&
document.getElementsByTagName){
     var btn=$('fetch');
     if(!btn){return};
     btn.onclick=fetchHTML;
   }
}
</script>
</head>
<body>
  <form>
    <input type="button" value="Fetch HTML" id="fetch" />
 
</form>
 
<div id="datacontainer"></div>
</body>
</html>

In this case, the "AJAX.Updater" object accepts, as the first parameter, the ID attribute that corresponds to the web page element where server results will be displayed as (X)HTML. Logically, if your web application expects a response served in this format, this object can be a real time saver.

Also, it's worthwhile to mention here that the "AJAX.Updater" object accepts the same arguments that were explained concerning the implementation of the "AJAX.Request" class, including the request method to be used, the set of variables sent to the server, and the respective callback functions that will be invoked during the different states of the request in question.

Okay, at this stage I have shown you how to take advantage of the functionality provided by AJAX when working with the Prototype library. Of course, this is only a simple introduction to the subject. If you want to dig deeper on this topic, please spend some time reading the official documentation.

Having covered basically the Prototype's AJAX module, in the following section I'll teach you how to use some additional features that are bundled with this library, including the new "Try.these()" function and the Ruby-styled "each" loop.

So, keep reading to learn more about these interesting topics.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials