10 Comments »

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.
<html>
  <head>
    <title>Ajax How To</title>
    <script src="prototype-1.6.0.2.js" type="text/javascript"></script>
     </head>
  <body>
	<p>
		<a href="javascript: void(0);" onclick="getSampleHTML();">call getSampleHTML()</a> 
		and display results in alert from sample code
	</p>
	<p>
		<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>
	</p>
	<p>
		<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>
	<p>Display the AJAX results below:</p>
	<div id="htmlResult">
	</div>
	<div id="xmlResult">
	</div>  
  </body>
</html>
  • Download the Prototype Javascript framework and save it to the ajaxHowTo folder (ex. C:\ajaxHowTo\prototype-1.6.0.2.js)
  • 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>
      <head>
        <title>Ajax How To</title>
        <script src="prototype-1.6.0.2.js" type="text/javascript"></script>
    	<script>
    
    	</script>
         </head>
      <body>
    
  • 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',   {     
    		method:'get',     
    		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>
    	<head>
    		<title>Ajax How To</title>
    	</head>
    	<body>
    		Hey, you just wrote some AJAX!!
    		<br/>
    		Wasn't that EASY!!!!
    	</body> 
    </html>
    

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

    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.

    UPDATE:


    Ajax Fast and Easy – part three is now available

     



    Tags: , , , , , , , , ,

    Trackback URL | Comments RSS

    10 Responses to “Ajax Fast and Easy – part two”
     

    […] Ajax Fast and Easy – part two […]

     

    Great article!
    I’ve been working on a JavaScript framework whose sole purpose is to guide people in their development.

    I’m trying emphasize convention over configuration and DRY principles. However, I’m so deep into the framework, that it is hard to explain its concepts the newbie user.

    After reading these two articles, it’s clear to me that you have this ability. We are looking for people like you to help us with this project – making JavaScript stupidly easy.

    If you are interested in helping with an open source project, this might be your opportunity. Let me know,

    Justin

    Justin Meyer wrote on February 24th, 2008 at 1:45 pm

     

    oh, we are starting to put up information on the library at:

    http://javascriptmvc.com

    Justin Meyer wrote on February 24th, 2008 at 1:46 pm

     

    […] Ajax Fast and Easy – part two […]

     

    Error: clearNode is not defined
    Source File: file:///C:/ajaxHowTo/index.html
    Line: 1
    It means that “clearNode()” fails in prototype.js
    I have search this function in prototype-1.6.0.2.js but don’t find it.

    Ignacio wrote on March 8th, 2008 at 4:32 am

     

    I didn’t actually go over the clearNode function in this article, however it is in the sample code that you can download to review the source. here is clearNode, add this to your script tag in the head…
    function clearNode(node) {
    $(node).innerHTML = “”;
    }
    it just clears out the text we added with the ajax call.

    webandnow wrote on March 8th, 2008 at 10:12 am

     

    I tried the sample code above and it doesn’t work in Vista Ultimate/IE7. Firefox 2.0.0.12 works fine. Any ideas?

    trumpy81 wrote on March 8th, 2008 at 12:50 pm

     

    i mention in the article that for it to work in IE, you need to run it on a webserver, with firefox you can just run it from the folder. if you want to test out the code on a webserver, just open up the sample page i provided here

    webandnow wrote on March 8th, 2008 at 1:57 pm

     

    Sorry, I forgot to mention that I was running the code under IIS 7.

    Anyhow, it turned out to be some hidden characters that were somehow copied along with the code.

    All is working now, just as it should.

    Thanks for the great tutorial BTW. It’s been fun and informative.

    trumpy81 wrote on March 8th, 2008 at 8:25 pm

     

    thanks for the clearNode code! Great little tutorial for a newbie! Many thanks for posting this!

    opally wrote on March 14th, 2008 at 1:38 pm


     




  • 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
  • trA¬nh duya»‡t 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