Dr. Mark Humphrys

School of Computing. Dublin City University.

Home      Blog      Teaching      Research      Contact

Online coding site: Ancient Brain

coders   JavaScript worlds


CA170      CA318      CA686      CA686I

Online AI coding exercises

Project ideas

Javascript Ajax

Ajax is not about XML

Browser-independent code

Javascript Ajax standards have varied across browsers. Can be tricky to write browser-independent, backward-compatible code.

One solution is to use other people's Javascript libraries/frameworks to take care of this complexity for you.

Use jQuery


Javascript Ajax demos

Simple pages. No header or footer. To make it easier to View Source.
Note source loads jQuery.

Ajax demo - fetch server file HTML JS
Ajax demo - fetch server file (minimal) HTML  
Ajax demo - call server program HTML JS
Ajax demo - call server program (minimal) HTML  
Ajax demo - fetch XML and parse HTML JS
Ajax demo - parse Flickr XML HTML  

Asynchronous programming

Coding Ajax takes some getting used to, because it is asynchronous.
You fire off a HTTP request. And then your code carries on.
At some future point the HTTP request returns.
When you make the request, you include the future function to call.

So you can't do:

var x = makeRequest ( url );  // get back data
// do stuff with x 
Instead you do something like:
var x;
makeRequest ( url, fn ); 
// don't do anything with x yet

function fn ( data )    	// called when request returns in future 
 x = data;
 // now can do stuff with x  

// and you may need multiple lines like the following in other parts of code:

 if ( typeof x == 'undefined' ) ...   // x has not yet been defined  
 else ...                             // x is defined

ancientbrain.com      w2mind.org      humphrysfamilytree.com

On the Internet since 1987.      New 200 G VPS server.

Note: Links on this site to user-generated content like Wikipedia are highlighted in red as possibly unreliable. My view is that such links are highly useful but flawed.