Posts Tagged ‘ajax’

‘Hello World’ using AJAX

Author: Jeremy Smyth

“Asynchronous Javascript And XML” is a mouthful for a web technology that’s in wide use these days. Simply put, it lets a web page download more content without reloading the page, and relies on a magic Javascript object called an XmlHttpRequest to do the work of requesting more data in the background, so it can fill in things on the page.

So, fiddling with AJAX, let’s do a little Hello Worlding:

Here’s a button, that when clicked, calls the “startRequest()” function in some javascript behind the entry:

<input onclick="startRequest()" type="button" value="Hello World!" />

And here’s a div:

<div id="ajaxtest"> <em>(although you can't see it as a separate div, this text will be replaced)</em></div>
(although you can’t see it as a separate div, this text will be replaced)

Go on, watch the div text above, and click the button.

Here’s the javascript:

var xhr; if (window.XMLHttpRequest){   xhr = new XMLHttpRequest(); } else if (window.ActiveXObject){ // in case it's IE   xhr = new ActiveXObject("Microsoft.XMLHTTP");  } // set up the callback xhr.onreadystatechange = function(){   myDiv = document.getElementById("ajaxtest");   if(xhr.readyState  == 4) {   if(xhr.status  == 200){    myDiv.style.background = "green";    myDiv.innerHTML =  xhr.responseText;   } else {    myDiv.innerHTML = "Error: " + xhr.status;   }   }   } function startRequest(){   xhr.open('GET', '/ajaxtest.php', true);   xhr.send(null); }

Firstly, the code sets up a variable xhr, which contains the magical object. This is the XmlHttpRequest object, and is responsible for communicating from the live Javascript to the back end server.

The request object is first set up at the script’s execution, but the only thing we do with it is tell it what to do when its “ready state” changes; that is, when the request shifts from not initialised, to set up, to having been sent, to being in process, to completion.

These states are numbered 0-4 respectively. Hold that thought; we’ll use it later. Suffice to say that, on the script’s execution, we now have an otherwise anonymous function that will be called when the ready state changes.

Now, in order for something interesting to happen, we click the button. That kicks off startRequest(), which in turn sets up a asynchronous request to ajaxtest.php, and submits it with no parameters (that’s the null). Asynchronous means we don’t sit around waiting for a response, and that’s why we need the readystatechange callback function.

So, when something interesting happens to the xhr object, the ready state changes (e.g. when we get a response back after sending our request). The browser then executes the anonymous function: first, the function checks that the request is complete – remember Ready State 4 above? don’t forget the ready state changes several times per request. We only want to read the response text once, at completion. Once the function has checked the request has been successfully completed, it then uses the xhr’s response-text to fill in the div called “ajaxtest”.

Et voila, we have “Hello World” in AJAX.

  • chad ochocinco career stats
  • bangles eternal flame mp3bengals forum
  • tea party lies
  • memo
  • vince young redskins
  • search engines for kids
  • randy moss combine results
  • chad ochocinco parents
  • arno
  • hp support quick test pro
  • astronomy
  • new england patriots underwear
  • new england patriots 1997 roster
  • dashboard
  • tea party table settings
  • bea exhibitors
  • c span 4 to 5
  • battleship hacked
  • hyosung
  • tea party zombies download
  • search 3 bodybuilding other index
  • bengals preseason schedule 2011
  • carwash
  • mtv rivals
  • bengals history
  • randy moss legal issues
  • zara phillips husband
  • vince young uncle rico
  • vince young dadvince young eagles
  • chad ochocinco and cheryl burke
  • trojans
  • search 50 cent
  • search engines other than google
  • chad ochocinco to detroit
  • la ink ink
  • textured
  • battleship aurora
  • tims
  • glaze
  • northfield
  • vince young released
  • canary
  • la ink 105
  • c span yesterdayc span zelaya
  • new england patriots espn blog
  • speak
  • new england patriots 3 4
  • zara phillips wedding date
  • fenwick
  • new england patriots gillette stadium
  • chicago bears 17 lisa lampanelli
  • federated
  • battleship ipad
  • infrared
  • comforters
  • tea party manifesto
  • chicago bears expo 2011
  • chad ochocinco quotes video
  • steering
  • bea per capita income
  • bengals arrests
  • disassembledis boards
  • nicu
  • dis quand reviendras-tu
  • dis tester
  • chad ochocinco ultimate catch cast
  • stronger
  • bengals undraftedbengals vs steelers
  • cspan ap government review
  • connecticut renaissance faire
  • powertrain
  • bengals hard knocks episode 1
  • hp support error 1005
  • c span youtube obama
  • hp support 6310hp support 7200
  • towns
  • tint
  • cspan facebook
  • la ink 3rd season
  • hangers
  • chicago bears gifts
  • vince young 10 11
  • digitizing
  • connecticut food bank
  • mtv executivesmtv fantasy factory
  • hp support contact number
  • tea party hobbits
  • greg olsen puzzles
  • bea 2011 map
  • chad ochocinco xpchad ochocinco youtube
  • bengals qb situation
  • randy moss college
  • c span shelby foote
  • pudding
  • zara phillips and the queen
  • rufus
  • tea party chicago
  • hp support chat
  • bea goldfishberg
  • randy moss 98 vikings
  • zara phillips school
  • dis pater
  • tea party 8 28 09
  • michelin
  • tea party medicare
  • sticky
  • chicago bears tattoos
  • chicago bears 08 record
  • vince young endorsementsvince young foundation
  • cspan presidents
  • battleship kirishima
  • search and seizure
  • zara phillips and the queen
  • mtv youtube channel
  • search 78search 800 numbers
  • battleship yamato wreck
  • mtv live
  • la ink phone number
  • tea party zombies download
  • beagle
  • battleship lexington
  • bengals merchandise
  • battleship excel
  • dist 95
  • greensboro
  • search xml file
  • dailymotion
  • bea fox
  • bea rims
  • hp support chat
  • bengals cats for sale
  • hp support id
  • bengals new uniforms 2012
  • polos
  • cspan washington correspondents dinner 2011
  • bridesmaid
  • vince young injury
  • xanadu bengals
  • randy moss korey stringer
  • chad ochocinco vs skip bayless
  • opera
  • bea 0b0 105
  • vince young yahoo stats
  • bengals insider
  • squadron
  • dis poem
  • vince young jay cutler
  • hp support 6500a plus
  • mtv 5 cover
  • nozzle
  • bea 4603
  • bengals forum
  • bengals football
  • hp support 530
  • battleship bismarck wreck
  • freida pinto jeansfreida pinto kissing
  • hp support hard drive replacement
  • mtv oddities
  • scored
  • tea party ribbons
  • christina
  • birthdate
  • vince young 6
  • mtv 90s music videos
  • zara phillips tongue
  • search vim
  • bea test
  • connecticut lottery
  • freida pinto 1995
  • randy moss vikings 2011
  • dis 2012 conference
  • vince young status
  • connecticut quarter error
  • dis boards cruise
  • connecticut state parks
  • new england patriots 07
  • search 5500
  • vince young z
  • search tumblr
  • vince young rumors
  • c span yesterdayc span zelaya
  • connecticut 97.7connecticut attorney general
  • bea oracle
  • randy moss bio
  • freida pinto boyfriend