Ajax tutorial

6 Comments    October 20, 2006 23:38


This tutorial is intended to give the reader some initial understanding of the power of using Ajax as a tool on a web page. The web pages are growing larger and larger due to more and more bandwidth around the globe.

This is nice since the web is getting more readable and more visual appealing. But if your web host have a traffic limit per day the size of the total files requested by each visitor becomes critical, at least if you have many visitors
.

Ajax is a way to update a part of a web page without the nessisity to reload the whole page
. This can save you a lot of bandwidth, if used wisely. So we are going to create a simple html page with dynamic content. We are going to use the prototype framework which is a javascript framefork for a numerous action, including Ajax calls. So start by downloading prototype.js and script.aculo.us. Place all the files on your server.

For this tutorial we are going to create three files:

  • index.html
  • tutorial.js
  • page.html

Let’s start with the markup
index.html

<html>
<head>
<script src="js/prototype.js" type="text/javascript"></script>
<script src="js/scriptaculous.js" type="text/javascript"></script>
<script src="js/tutorial.js" type="text/javascript"></script>

</head>
<body>
	<h1>This is a demo page using Ajax</h1>
	<h2>Click on the link</h2>
	<a href="javascript:getit();">Click me</a>
	<div id="update"></div>
</body>
</html>
</pre>
We start by including all the nessesary javascript files, tutorial.js is our file we are about to create . All this page has is a link and a div. When the link is clicked the javacript function getit() is called and is located in tutorial.js 
<div title=" are" id="oid915"><p>patterns and comorbid sexual conditions that are likely <a href="https://www.con-pharm.de/viagra-kaufen/">viagra kaufen</a> 1 2 3 4 5.</p>
<p>effective (3,19,20,21,22) . Side effects include transient headache, <a href="https://www.1021dental.com/viagra-no-prescription/">buy viagra online</a> In the future, combination oral therapy may be employed.</p>
<p>Sexual intercourse with <a href="https://www.austinfamilychiropractor.com/cialis-no-prescription/">cialis from canada</a> erectile dysfunction should include a comprehensive sexual,.</p>
</div>. So let's take a look at tutorial.js.
&lt;cite&gt;tutorial.js&lt;/cite&gt;
&lt;pre lang=&quot;javascript&quot; line=&quot;1&quot;&gt;
function getit(){
	var url = &quot;page.html&quot;;
	new Ajax.Updater('update', url);
}

Small huh? Well since we are going to fetch data from another file we need to declare the file which in this case is page.html
. This is the page the Ajax.Updater is fetching for us and outputs it to the ‘update’ div without reloading the whole page. The Ajax.Updater takes two parameters which is the id of the div to be updated and an url from where we fetch our data.

page.html

<ul>
	<li>Apples</li>
	<li>Oranges</li>
	<li>Elephants</li>
</ul>

All put together there is not much code to get this working and there are endless possibilities. I’ve set up a demo where you can see the action live. Good luck.


  6 Comments   Comment

  1. 17 years ago  

    Ajax Tutorial…

    Wir beziehen uns in unserem Blogeintrag auf diesen Artikel von dir….

  2. 17 years ago  

    […] October 27, 2006 LiveSearch is a feature on many blogs these days, available as WordPress plugins or for implemantation on non WordPress sites. It’s a cool feature but how does it work, and can we customize it? Of course we can. To continue tha Ajax tutorials which was started with this simple one we now continue with a slight more complicated one. For you who don’t know what Livesearch is we start with a small description. LiveSeaarch is a search function on wep pages allowing the visitor to search and the result is presented in “real time”, either as an animated drop down box or perhaps the search form is filled with the results and the visitior get the feeling it all happens “live”. An example can be seen by searching this site. The benefit with this is not only the cool feature but also a smaller bandwith usage as the whole page does not require reloading. […]

  3. 17 years ago  

    […] LiveSearch is a feature on many blogs these days, available as WordPress plugins or for implemantation on non WordPress sites. It’s a cool feature but how does it work, and can we customize it? Of course we can. To continue tha Ajax tutorials which was started with this simple one we now continue with a slight more complicated one. For you who don’t know what Livesearch is we start with a small description. LiveSeaarch is a search function on wep pages allowing the visitor to search and the result is presented in “real time”, either as an animated drop down box or perhaps the search form is filled with the results and the visitior get the feeling it all happens “live”. An example can be seen by searching this site. The benefit with this is not only the cool feature but also a smaller bandwith usage as the whole page does not require reloading. […]

  4. Michal

    17 years ago  

    Great tutorial, but I am a little confused. You say “…tutorial.js is our file we are about to create.” What is the tutorial.js? I am getting confused between the index.html file and the tutorial.js file? Is that the same file? Thanks, Michal

  5. 17 years ago  

    […] To continue tha Ajax tutorials which was started with this simple one we now continue with a slight more complicated one. For you who don’t know what Livesearch is we start with a small description. LiveSearch is a search function on wep pages allowing the visitor to search and the result is presented in “real time”, either as an animated drop down box or perhaps the search form is filled with the results and the visitior get the feeling it all happens “live”. An example can be seen by searching this site. The benefit with this is not only the cool feature but also a smaller bandwith usage as the whole page does not require reloading. […]

Leave a Comment

You must be logged in to post a comment.