Ajax basics using Prototype JS

Ajax basics using Prototype JS

Below are some codes that will help you on how to use Ajax. This is a great tool to create a more flexible web pages and enhance user interaction without reloading the page. Most of the popular websites are now using ajax i.e. google, facebook, yahoo, youtube, twitter and more.

First, we need an ajax framework library for easier use, there are lots of ajax library to use, you can use JQuery, Mootools, Dojo etc. I’m a using Prototype AJAX API library that are hosted on google servers.

http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/prototype.js

Example:

Create a <div></div> tag on your HTML

<div id="content_one"></div>

Then insert this code below:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/prototype.js"></script>

<script type="text/javascript">
new Ajax.Request(strURL,
{
method:'post',
onSuccess: function(r){

var temp1 = r.responseText; //fetch the data from strURL
$('content_one').innerHTML = temp1; //put the data on the document elements

},
onFailure: function(){ alert('Internet Connection Problem'); }
});

/* End of file */
/* http://www.archiemercader.com/ */
</script>

More information about AJAX:

http://en.wikipedia.org/wiki/Ajax_framework

32% off New Product orders at GoDaddy.com! Expires 7/30/13.

Leave a Reply