Training, Open Source computer languages

PerlPythonMySQLTclRubyC & C++LuaJavaTomcatPHPhttpdLinux

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Ajax - going Asyncronous and what it means

This is the second part of an "easy Ajax" series - for the first installment [link here].

In the first installment, I showed you a "bare bones" example in which Javascript on a browser called up a web service to change information being displayed dynmaically - but I couldn't reall call it Ajax (more "Jax") as it wasn't asyncronous. So

• What does "Asyncronous" mean in this context?

• What is the advantage of using "Asyncronous"?

• How do we achieve it?

What does "Asyncronous" mean in this context?

It means that the request for data to be supplied by the web service is made, but then the browser gets on with other activities rather than waiting for the response which could - in some cases - take a few seconds to arrive if it involves a complex SQL query or a lot of information being transferred.

What is the advantage of using "Asyncronous"?

It means that the user can continue to complete forms and interact with the display in his browser without it appearing to lock up while a response from the server is awaited, and it means that several requests can be active at the same time.

How do we achieve it?

We change the flag on the XMLHttpRequest object's open method to true to indicate that we want to have the send method return to us right away rather than awaiting completion:

rqo.open('GET',qry,true);
rqo.send(null);


We add in an extra assignment - setting the XMLHttpRequest object's onreadystatechange method to indicate a callback to a function:

rqo.onreadystatechange = handlelang;

And finally, we supply that callback function:

function handlelang() {
document.getElementById("result").innerHTML =
    rqo.responseText + " (via Ajax)";
}


IF you are using a compliant browser, that should be all the changes you have to make to yesterday's example; the complete source code is available [here], the web service source - unchanged from yesterday's example - [here] and you can run the code [here].

But you note I said "IF" - and it's a big if! Unfortunately, there are browser specific issues involved with most Javascript applications, and although the code I have given you will work, and work well, on most modern Mozilla based browsers such as Firefox (and Safari and Iceweasel and so on), it will NOT work on Internet Explorer, nor on older browsers, nor on browsers where the user has elected to turn Javascript off. And we haven't worked out what we should do if the web service isn't available when we want it either!

We can fix the big issue of Microsoft's Internet Explorer, by sensing which browser we're using and adding in conditional code that uses their alternative objects ... but that going to obfurscate the code, and is something I'll come back to in my third posting in this series, where I'll also look at the return status from the request so that we can deal with slow (or stopped or gone away) servers cleanly.

(written 2008-09-28 08:09:28)

 
Associated topics are indexed under
W510 - Web and Intranet - Executable Content
W602 - Web and Intranet - Client Side Technologies
H307 - PHP - Web2 and caching

Back to
Starting Ajax - easy example of browser calling up server data
Previous and next
or
Horse's mouth home
Forward to
Javascript/HTML example, dynamic server monitor
Some other Articles
Marc Schneider is still having email trouble
Holt on holt
Hotel Guest Surveys
Javascript/HTML example, dynamic server monitor
Ajax - going Asyncronous and what it means
Starting Ajax - easy example of browser calling up server data
Alternative URLs using % symbol encoding
Middle aged subsidise young and old
Coming home tonight
We love children ... but our hotel is not going to be their scene
1889 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho