In AJAX Fast and Easy – part one, we defined AJAX and decided it wasn’t really that complicated.
In AJAX Fast and Easy – part two,
we created an index.html file, a backendHTML.html file and we downloaded the
Prototype Javascript framework. We put them all together and made some
ajax magic happen. However, in part 2 we left out all the junk about XML, and all we really did was display
the ajax results as an alert. Not too useful, but it proved the point and it was pretty fun.

Today we are going to kick some ajax ass. We are going to get it to do all kinds of useful junk for us. First
we will make an ajax call to our backend page and stuff that response into our index page. Then we will
make an actual XML file, call it with ajax, parse the XML and render the data on our index page. By the time we
are done, you will have some pretty functional code that you will be able to use on your own projects.

Ready to get going? Ok, make sure you got all your files from part one and two in C:\ajaxHowTo\. If you lost them,
or you didn’t do part one or two, or you’re just lazy, you can
download a zip file
with all of the source code already written for you.

Just like we did in part two, we are going to take this step by step.

  • Open index.html for editing. We are going to start by writing a new function for another ajax call. Add the code from the codebox below inside the script section of index.html, just below our getSampleHTML function.
  • function getHTML() {
    	new Ajax.Request('backendHTML.html',   {     
    		onSuccess: getHTMLSuccess,     
    		onFailure: getHTMLFailure

    You’ll notice that the only difference between this function and our first function, getSampleHTML, are the onSuccess
    and onFailure pieces. What we are doing here is telling the ajax call to call two new functions,
    that we haven’t written yet, on a success or failure.

  • Now create two new functions called getHTMLSuccess and getHTMLFailure as shown in the code below.
  • function getHTMLSuccess(originalRequest) {
    	var response = originalRequest.responseText;
    	$('htmlResult').innerHTML = response;
    function getHTMLFailure() {
    	alert('woops, what happened?');

    Lets take a minute to figure out what it is we are trying to do here.

    We wrote the new ajax call and named it getHTML, nothing new, we only changed the onSuccess and onFailure calls.
    Then we created the getHTMLSuccess function. What this function does is, it accepts the ajax response as originalRequest as you can see in getHTMLSuccess(originalRequest). We create a variable named response and set
    it equal to the response text. The response text will be the html that gets sent back, just like in our alert from
    part two. The difference here is instead of displaying the response in a javascript alert, we are assigning it
    to a variable and stuffing the html response into our ‘htmlResult’ div further below in the body of our html.
    Are you asking “where the hell did we do that?” No problem, I kind of snuck it in there. The prototype
    framework has some cool special shortcuts for doing everyday javascript junk. In this case, we have the $ sign.
    The $ sign with prototype is used as a shortcut for “document.getElementById(‘htmlResult’)”. If it still doesn’t
    make sense, just take my word for it. The $(‘htmlResult’).innerHTML = response; line will insert the ajax
    response into our htmlResult div and the content on the page will be magically updated.

    The second function we wrote is getHTMLFailure. As you can tell, its only job is to show an alert if a failure occurs.
    Normally, under these circumstances, the only time you can get a failure is if the backendHTML.html file can’t be found.

  • So, what’s next? Well, nothing really, we just wrote some kick ass ajax code that will go get the contents of any
    file we decide to put in the Ajax.Request(‘backednHTML.html’) section of our function. If we open up index.html and
    click on ‘call getHTML()’ we should see the text on the page change and the contents of backendHTML.html should
    magically appear at the bottom of the page.
  • That’s ajax man! That’s really all there is to it. Just like in part two, we use prototype to
    make an ajax call to our backend page, but instead of showing the response in an alert, we display it on the page.
    Much more useful if you ask me.

    Now it’s time for us to put it in high gear and see what this ajax stuff is really all about and how it got its name.

  • Open your favorite text editor and make a new document. Enter the code displayed in the code block below, and save the
    file as backendXML.xml in your ajaxHowTo folder.
  • <?xml version="1.0" encoding="UTF-8"?>
    		<title>The Shining</title>     
    		<author>Stephen King</author>     
    		<title>Fahrenheit 451</title>     
    		<author>Ray Bradbury</author>     
  • We will create a new ajax function to call this new file. Open up index.html and create a new function just like the one
    in the code box below. This function is the same as the getHTML function we just wrote, we only change the URL we call,
    and the onSuccess function name.
  • function getXML() {
    	new Ajax.Request('backendXML.xml',   {     
    		onSuccess: getXMLSuccess,     
    		onFailure: getHTMLFailure
  • Next we write our most complicated function of the entire project. Add the function in the code box below to index.html,
    and we will walk through what it’s doing line by line. Don’t worry, it will make sense, but it is some pretty complicated
    stuff. Javascript is like ice skating, when it’s done well, it looks easy. And, I think it should be.
  • function getXMLSuccess(originalRequest) {
    	var response = originalRequest.responseXML;
    	var numberOfBooks = response.getElementsByTagName('book').length;
    	var titles = response.getElementsByTagName('title');
    	var authors = response.getElementsByTagName('author');
    	var isbns = response.getElementsByTagName('ISBN');
    	var resultText = "";
    	resultText = "Total number of books = " + numberOfBooks;
    	for( i=0; i<numberOfBooks; i++ ) {
    		currentTitle = titles[i].firstChild.nodeValue;
    		currentAuthor = authors[i].firstChild.nodeValue;
    		currentIsbn = isbns[i].firstChild.nodeValue;
    		resultText = resultText + "<p>";
    		resultText = resultText + "<br/> Book " + (i+1);
    		resultText = resultText + "<br/> Title = " + currentTitle;
    		resultText = resultText + "<br/> Author = " + currentAuthor;
    		resultText = resultText + "<br/> ISBN = " + currentIsbn;
    		resultText = resultText + "</p>";
    	$('xmlResult').innerHTML = resultText;

    Just like the earlier function we wrote, getHTMLSuccess, this new function accepts originalRequest as the ajax response.
    We assign the response to a variable named response, but this time we use originalRequest.responseXML instead of
    responseText. That part is important and you must use responseXML if we are going to parse the XML file to extract the
    data we want from it.

    The reason we would do this is because, unlike making an ajax call to an HTML file and displaying the entire file,
    we want to call an XML file and only extract the specific data that we want from it. Remember in part one, we said that
    the XML nodes only define what the data means? Well, the tag doesn’t mean anything to an HTML file, so why would
    we want to render that on our HTML file? We wouldn’t, but we would probably want to render the book name and that’s
    what we are about to do.

    First we need to find out how many books we have, so take a look at the numberOfBooks variable. This variable counts the
    book nodes. In this case there are two. The next variable is titles, this variable gets all the nodes named title from
    the XML. The same goes for authors, and isbns.

    We define an empty string called resultText that will be a place holder for some HTML we will build in a minute. We then
    start adding text to the new resultText string to display the total number of books found on the XML file.

    Next we create a loop to repeat the same procedures for each book, in this case we will loop twice. Our loop creates
    three variables and assigns the node values to them. I’ll give one example of how it works and the rest should be self-explanatory. currentTitle = titles[i].firstChild.nodeValue means that on the first round of the loop we are looking
    for titles[0].firstChild.nodeValue, which means… Give me the nodeValue, in this case ‘The Shining’ of the first item
    in the XML file named title (<title>The Shining</title>).

    The rest of the script just does the same thing over and over again for as many books as we have in the XML file. We get
    the values from the XML, we assign them to variables, we attach the values to the resultText variable, we add some
    normal HTML junk around them to make them look pretty, then we do the prototype magic again.

    We use the $ sign to update the div xmlResult with the text we just created in our resultText variable. Again, this is
    nothing new, its simply document.getElementById(‘xmlResult’).innerHTML = “the junk we just looped through”.

  • Open your index.html file in the browser and click on ‘call getXML()’. You should see the ajax magic happen before your
    eyes and text should magically appear on the page. Again, if you don’t feel like doing any of this, just view the
    sample page that I made and click the link.
  • That example took a little longer to explain, but that is pretty much everything you need to know about ajax.

    Page A makes an AJAX call to page B, and page A displays the content of page B.

    If you want to call an HTML page, fine. If you want to call an XML file to extract data from, fine. Either way, you
    have a page that makes a call to a backend page. Normally you will have some sort of server side technology on the
    backend page like PHP or java that runs database queries and gets dynamic content, but that has nothing to do with
    ajax itself.

    I am a huge fan of the Prototype Javascript framework, and
    there are ton of other shortcuts like the $ sign built into it. There are also other ways to make ajax calls with prototype
    that actually make the job we just worked even easier, but for the sake of the explanation, we didn’t use them. I
    will try to put together another post that goes over some of the most useful things you can do with prototype in the future.

    Here is a cheat sheet for prototype
    that you will probably find useful.

    Well, that’s it. Now you know ajax. It’s no big secret, it’s not complicated, and it’s pretty fun.

    What would you like the hear about next?


    Tags: , , , , , , , , , ,

    Trackback URL | Comments RSS


    In “AJAX Fast and Easy – part one“, we defined AJAX. We said the Asynchronous part could be compared to opening a link in a new window so that you don’t leave your current page. We said the Javascript part could be as simple as adding a call to the freely available Prototype Javascript framework. We decided that XML was pretty simple to understand because all it does is describe the data you put in it, and we even went as far as saying that XML is not necessary when writing AJAX.

    The first thing we will do in part two is prove that last statement. XML is not necessary to write AJAX. So, without further ado, take out your notepads, or your favorite HTML editor and get ready to write some AJAX code. If you are running a web server, great, but if not, don’t worry about it. If you have Firefox installed, you can run all of this code without a web server.

    Lets go about this step by step, so we can see how easy it really is.

    • Create a new directory for our files, and name it ajaxHowTo (ex. C:\ajaxHowTo\).
    • Create a new file in C:\ajaxHowTo\ named index.html.
    • Open index.html for editing and enter the code below to create a simple web page.
        <title>Ajax How To</title>
        <script src="prototype-" type="text/javascript"></script>
    		<a href="javascript: void(0);" onclick="getSampleHTML();">call getSampleHTML()</a> 
    		and display results in alert from sample code
    		<a href="javascript: void(0);" onclick="getHTML();">call getHTML()</a> 
    		and display results in a div below 
    		<a href="javascript: void(0);" onclick="clearNode('htmlResult');">clear</a>
    		<a href="javascript: void(0);" onclick="getXML();">call getXML()</a> 
    		and display results in a div below 
    		<a href="javascript: void(0);" onclick="clearNode('xmlResult');">clear</a>
    	<p>Display the AJAX results below:</p>
    	<div id="htmlResult">
    	<div id="xmlResult">
  • Download the Prototype Javascript framework and save it to the ajaxHowTo folder (ex. C:\ajaxHowTo\prototype-
  • Feel free to open up your index.html file and view it in a web browser. The links won’t do much right now because we haven’t written the javascript functions yet, but it should look like this. For the very lazy, I have packed all the sample code in a zip file for download.

  • Inside the head section of the html file, create another script section so it looks like this.
  • <html>
        <title>Ajax How To</title>
        <script src="prototype-" type="text/javascript"></script>
  • Inside of the new script block, we will now create our first Javascript function and name it getSampleHTML(). This function is almost an exact copy of the sample from the prototype examples found at www.prototypejs.org/learn/introduction-to-ajax. The only difference is in the html file we call in the Ajax.Request section.
  • function getSampleHTML() {
    	new Ajax.Request('backendHTML.html',   {     
    		onSuccess: function(transport){       
    			var response = transport.responseText || "no response text";       
    			alert("Success! \n\n" + response);     
    		onFailure: function(){ 
    			alert('Something went wrong...') 
  • Just like steps 2 and 3, create a new html file and name it backendHTML.html. Make it look like the code block below and save it to the ajaxHowTo folder.
  • <html>
    		<title>Ajax How To</title>
    		Hey, you just wrote some AJAX!!
    		Wasn't that EASY!!!!

    We should now have 3 files inside of our new folder /ajaxHowTo/. We should have index.html, backendHTML.html, and prototype-

    That is all we need. We just wrote our first AJAX code! Wasn’t that easy? Just two HTML files and one javascript function!

    Now, go ahead and open index.html. Click on the first link labeled “call getSampleHTML()”. Clicking that link causes a call to our Javascript function named getSampleHTML(). That function calls a function built into Prototype called Ajax.Request(). Ajax.Request() gets passed the name of the file we want to call via AJAX, in this case “backendHTML.html”. In the onSuccess section of our function, we specified that we want an alert to appear with the words “Success!” followed by the content of the file we called through AJAX.

    Thats all there is to it!

    It’s as simple as 1,2,3!

    Page A makes an AJAX call to page B, and page A displays the content of page B.

    Thats AJAX!

    That concludes “Ajax Fast and Easy – part two”. In part three, we will expand on these files by adding the ability to render the AJAX response on the current page as HTML, and we will make a call to an actual XML file, parse the XML and render the XML response on the page as well. Don’t worry, I promise it will be fast and easy.


    Ajax Fast and Easy – part three is now available


    Tags: , , , , , , , , ,

    Trackback URL | Comments RSS


    The purpose of this post is to try to explain AJAX the way I understand it. To me,
    AJAX is not some complicated technology that you need to go out and buy books to
    learn. Rather, it is simply two or more web pages, and a couple of simple Javascript
    functions.I have come to hate the term AJAX. It seems every time I mention AJAX, either
    developers get nervous, or account managers get excited. Neither situation benefits me.
    Actually, I’ve pretty much stopped using the term at all. Depending on my audience, I’ll
    make up different terms. Like when I’m talking to Java developers, I’ll say “we need to do
    a back end call” and that doesn’t freak them out, but telling them we need to do an AJAX
    call makes them sweat.

    Hopefully, if enough people read posts like this, the mystery and glamor will be removed
    from the term AJAX. So, lets get to the de-mystification.

    First, lets dissect the name itself. A.J.A.X.

    A = Asynchronous

    J = Javascript

    A = And

    X = XML

    Most developers reading this will feel pretty comfortable with the Javascript part.
    It’s the asynchronous and the XML parts that make most people nervous. They really
    shouldn’t. I’ll briefly describe each, then we’ll put it all together.

    Asynchronous simply means “lets open another URL without changing or reloading the page.”
    Picture it like this… You are reading a blog post, and you come across a link to another page.
    You don’t want to stop reading, so you right click the link and open it in a new window or another
    tab right? Now you continue reading the page you were on. Asynchronous simply means we are doing
    something else without disturbing the page we are viewing.

    Javascript, we all know what that is I hope. If you don’t, then you should probably be reading
    some other sites first (intro to Javascript).
    Now, the purpose of this post is to make AJAX easy to write, and easy to understand, so we are
    going to use Javascript tools that make our job a lot easier. Whenever I write AJAX code, I always use
    the Prototype Javascript framework.
    Go ahead and download it, we’ll talk more about it later, just know that it has most of the Javascript
    we will need built in.

    Before I get into XML, and what it is, I would like to note that AJAX doesn’t even need XML to work.
    For those who can’t or won’t use XML, don’t worry, you can still write useful AJAX code without it, but
    please continue reading and maybe you will decide that you can use XML after all. It does make AJAX a
    lot easier, but like I said, it’s not necessary.

    XML shouldn’t be scary, but many people just don’t know what it is. If you’ve got this far and you
    aren’t totally confused, then you probably know how to write HTML. HTML and XML should not necessarily
    be compared, but they are similar in how they are written. XML is written using ‘tags’ similar to HTML.
    And XML tags need to be “balanced” just like HTML should be. Following is some simple HTML:


    Most people will recognize this as the most basic of HTML code, but what you may not know is that
    this could also be valid XML code as well. I mentioned the word “balanced” earlier. In the sample
    above, you will notice that each tag has a corresponding ending tag like <html></html>.
    That is what balanced means. That is probably the most important concept in XML. Unlike HTML, we don’t
    necessarily use XML for presentation, XML is normally used more like a database. For example, if the
    code above were an XML file, and we wanted to think of it as data, we could say that the <html>
    section could be the database itself, the <head> section could represent a table in that database,
    the <title> section could represent a column in that table and the word “sample” would be the value
    of that column. If it were a database, we could query it something like this…

    select title
    from html.head

    We could spend a great deal of time explaining XML, but lets just says that its basic function is to
    store data and to define what that data means. So, in the example above, the word “sample” means, and
    we know this because of the XML tags we use to surround it, it is the ‘title’ of the ‘head’ of the ‘html’ file.

    I’ll write one more sample XML just for clarification because I think I’ve spent too much time on
    this subject already, but I think one more sample will be helpful in describing just how easy XML is.
    Lets say we want to get a list of books. We could represent that list of books with XML.

        <title>The Shining</title>     
        <author>Stephen King</author>     
        <title>Fahrenheit 451</title>     
        <author>Ray Bradbury</author>     

    From that example, XML should be pretty self explanatory. And that is the real point of XML.

    That concludes “AJAX Fast and Easy – part one”. In part two, we will build a basic HTML file
    and make an AJAX call to another basic HTML file, leaving the XML part out of the equation.


    Ajax Fast and Easy – part two is now available


    Tags: , , , , , , , , ,

    Trackback URL | Comments RSS


  • songs of agent vinod
  • download enrique iglesias escape video
  • jboss as community download page
  • mp3 of jannat 2 songs
  • peter john young folks mp3
  • kala doria song free
  • shin chan mp3 download
  • sorry board game
  • jaws theme song download mp3 skull
  • phir wohi raat hai song download
  • minecraft map er mod
  • fazer download do ares em portugues gratis
  • end of all time
  • anjunabeats volume 9 mixed by above & beyond free
  • soy incondicional prince royce download
  • trAnh duyat opera
  • a histA?ria de ester completo
  • el malo remix mp3
  • bound by darkness alexis morgan free download
  • les miserables 25th anniversary cd free
  • let me down mp3
  • seikon no qwaser mp4
  • rashid bhikha nasheed download
  • orange range hana free download
  • sims 2 custom content no s folder
  • portal 2 pc ita iso
  • jagjit singh mp3 download songs
  • vga driver windows xp download free
  • lagu jawapan cintaku sedalam lautan atlantik
  • mp3 software free youtube
  • windows 7 s folder takes long time
  • 6 8 12 download brian mcknight
  • prezioso feat marvin song 2 download
  • download theme os 7 for blackberry 9780
  • tds rates for ay 12 13
  • iqbal bano songs mp3 free download
  • ultimate dvd burner
  • grupo bom gosto 2012 cd
  • online movies download hindi new
  • video songs of yes boss
  • carter 4 deluxe download zip
  • http er free
  • cnet downloads for windows 7
  • interviul vietii mele free
  • world championship rugby ps2
  • costi ionita si sahara champagne
  • tom jerry movie free download cartoon
  • cs 1.6 for kids download free
  • download do nero vision em portugues
  • how to a video from youtube for free
  • garth brooks against the grain mp3 download
  • windows 8 consumer preview folder
  • battlefield vietnam demo free
  • mysql client for linux
  • stela enache & florin bogardo ani de liceu fisierul meu
  • crossfire multi hack v1.6.dll
  • serious sam ii download full game
  • bb storm facebook
  • katekyo hitman reborn episode downloads
  • sete de noi ellie white
  • tom zA blog download
  • fifa 13 polski komentarz chomikuj
  • download de che mu xanh aoe 1
  • marley e eu download legendado avi
  • sve mape za cs 1.6 download free
  • festival 2012 black sabbath video
  • dolby control center.msi
  • download sleep machine app
  • slightly stoopid closer to the sun album
  • oracle golden gate software free
  • the darkest kiss free download
  • iphone 4 ipsw 5.1.1
  • 4minute hot issue song
  • ios 5 downloaden op ipod touch
  • gustavo lima balada lyrics download free
  • silverlight 4 tools for vs 2010 and wcf ria services download
  • star wars 3 movie
  • kanon standard edition english download
  • some nights free mp3 download fun
  • uae yellow pages 2010 download
  • directshow decoder 8192 download free
  • song bole chudiyan with video
  • gantung melly goeslow free mp3
  • claw game free full version for mac
  • racionais 1000 trutas mil tretas download dvd
  • down on me hulk
  • amazing grace sheet music download free
  • la roux bulletproof rar download