Web Design Tips and Tricks

 

AJAX Should You Believe the Hype?

AJAX: Should You Believe the Hype?

There's a craze on the web at the moment, and that craze is named AJAX. What's that? Well, according to some people, it's a technology that's going to redefine the web.

What Does AJAX Stand For?

AJAX means 'Asynchronous Javascript and XML'. Adaptive Path came up with the term in this essay: http://www.adaptivepath.com/publications/essays/archives/000385.php . However, the word is really a bit of a misnomer: AJAX doesn't really rely on XML at all, but rather on a Javascript function that happens to be named XMLHttpRequest.

All About XMLHttpRequest.

XMLHttpRequest was originally invented and implemented by a Microsoft team who were working on a webmail application, and it's been around for a while now (since 1999, in fact). The reason it has suddenly come to prominence now is that Google used it in three projects, Gmail, Google Suggest and Google Maps, and in each case managed to create a much better user interface than they would have been able to otherwise.

So what does XMLHttpRequest do? To put it simply, it lets your Javascript go back to the server, fetch some new content, and write it back out onto the page – all without the user having to change pages. This gives a 'smoother' feel to web applications, as they can do things like submit forms without the whole browser window needing to go blank and reload the page. Take a look at maps.google.com now, and notice how you can drag the map around anywhere you want to go without having to reload the page. This would be unthinkable without XMLHttpRequest.

One of the biggest reasons XMLHttpRequest has become popular now is that browsers other than Internet Explorer have started to support it, mainly due to the fuss over its use in Gmail. The function is, by all accounts, a very simple one in technical terms: it was just a matter of someone having the idea.

Getting Started with AJAX.

The first thing to do to get started with AJAX, then, is to create an XMLHttpRequest object in your Javascript code. As ever, Internet Explorer has to be difficult, doing this a different way to every other browser out there. That means that you should use this Javascript code:

var ajax;
onload = function () {
if (window.ActiveXObject) {
ajax = new ActiveXObject("Microsoft.XMLHTTP");
} else {
ajax = new XMLHttpRequest;
}
}

What you've done there is declared ajax as a global variable (usable by all functions), and then created a new AJAX object when the page loads, either using ActiveX for Internet Explorer or the XMLHttpRequest function in other browsers.

The next step is to write two more functions: one to get data from a URL, and one to handle the data that comes back.

function getText(url) {
ajax.open("GET", url, true);
ajax.send(null);
}

ajax.onreadystatechange = function () {
if (ajax.status == "200") {
// do things with retrieved text (in ajax.responseText)
}
}

All you need to do then is call getText from the relevant part of your code with a URL you want to get text from, and put that text wherever you want it to be in the state change function.

The most powerful thing about this approach is that the URL you send to the server can contain variables, using the old REST (question marks and equals signs) way of doing things.

That means that, using the code above, you can easily send a request to ajax.php on your server, and include the current value of, say, an input box for a username on a registration form. The ajax.php script checks whether that username is already in use, and then sends either '1' or '0' as its only output. All you would then need to do is check whether ajax.responseText was 1 or 0, and change the page accordingly, preferably using getElementByID.

Should I Use It?

The question remains over just how useful this is: it's certainly good for some applications where users would otherwise have to submit data over and over again, but it's not much use for smaller ones. AJAX coding has a tendency to take a lot of time, especially the first time you try it, and this is often time that could have been better spent on other parts of the project. In short, don't be afraid of AJAX, but make sure you use it when you find a project that's well-suited to asynchronous transfer – don't go trying to fit AJAX to a website that doesn't suit it, just because you think it's cool.

 

 Article index
 
5-Simple-Steps-to-Accepting-Payments
5-Steps-to-Understanding-HTML
10-Easy-Ways-to-Promote-Your-Website
5-Ways-to-Avoid-the-1998-Look
6-Reasons-Why-You-Need-a-Website
7-Ways-to-Make-Your-Web-Forms-Better
Ads-Under-the-Radar-Linking-to-Affiliates
AJAX-Should-You-Believe-the-Hype
All-About-Design-Principles-and-Elements
An-Introduction-to-Paint-Shop-Pro
An-Issue-of-Width-the-Resolution-Problem
A-Question-of-Scroll-Bars
Avoiding-the-Nuts-and-Bolts-Content-Management-Software
Beware-the-Stock-Photographer-Picking-Your-Pictures.
Building-a-Budget-Website
Building-Online-Communities
Clean-Page-Structure-Headings-and-Lists
ColdFusion-Quicker-Scripting-at-a-Price
Column-Designs-with-CSS
Content-is-King
CSS-and-the-End-of-Tables
Cut-to-the-Chase-How-to-Make-Your-Website-Load-Faster
Designing-for-Sales
Designing-for-Search-Engines
Dont-Be-Scared-Its-Only-Code-HTML-for-Beginners
Dreamweaver-The-Professional-Touch
Encryption-and-Security-with-SSL
Finding-a-Good-HTML-Editor
Focus-on-the-User-Task-Oriented-Websites
Fonts-are-More-Important-Than-You-Think
Free-Graphics-Alternatives
FrontPage-Easy-Pages
Hints-All-the-Way
Hiring-Professionals-5-Things-to-Look-For
How-Databases-Work
How-the-Web-Works
How-to-Get-Your-Website-Talked-About-on-Blogs
How-to-Install-and-Configure-a-Forum
How-to-Make-Visitors-Add-You-to-Their-Favorites
How-to-Run-Ads-Without-Driving-Visitors-Crazy
How-to-Set-Up-Your-Hosting-in-5-Minutes-Flat
IIS-and-ASP-Microsofts-Server
Image-Formats-GIF-JPEG-PNG-and-More
Its-a-World-Wide-Web-Going-International
JSP-Java-on-Your-Server
LAMP-The-Most-Popular-Server-System-Ever
Making-Friends-and-Influencing-People-the-Importance-of-Links
Making-Searches-Simple
Offering-Free-Downloads-on-Your-Website
Opening-a-Web-Shop-with-E-Commerce-Software
Perl-Cryptic-Power
Photoshop-a-Graphic-Designers-Dream
PHP-Easy-Dynamic-Websites
Picking-a-Colour-Scheme
Printing-and-Sending-the-Two-Things-Users-Want-to-Do
Putting-Multimedia-to-Good-Use
Python-and-Ruby-the-Newer-Alternatives
Registering-a-Domain-Name
Registering-Your-Users-by-Stealth
RSS-Really-Simple-Syndication
Setting-Up-a-Mailing-List
Setting-up-a-Test-Server-on-Your-Own-Computer
Some-Places-to-Go-For-More-Information
Taking-HTML-Further
Taking-HTML-Further-with-Javascript
Taking-Your-Website-Mobile
Text-Ads-Unobtrusive-Advertising
The-5-Principles-of-Effective-Navigation
The-Art-of-the-Logo
The-Basics-of-Web-Forms
The-Basics-of-Web-Servers
The-Case-Against-Flash
The-Confusing-World-of-Web-Hosting-Making-Your-Decision
The-Evils-of-PDFs
The-Importance-of-Validation
The-Many-Flavours-of-HTML
Theres-More-than-One-Web-Browser
The-Smaller-the-Better-Avoiding-Graphical-Overload
The-Top-10-Biggest-Web-Design-Mistakes
The-Web-Designers-Toolbox
The-Web-is-Not-Paper
Time-for-User-Testing
Titles-and-Headlines-Its-Not-a-Newspaper
Tracking-Your-Visitors
Understanding-Web-Jargon
Uploading-Your-Website-with-FTP
Using-Flash-Sensibly
Using-Quizzes-and-Games-to-Get-Traffic
VBScript-Javascript-Made-Easy
Websites-and-Weblogs-Whats-the-Difference
What-Do-You-Want-Your-Website-to-Do
What-You-See-Isnt-Always-What-You-Get
Which-Database-is-Right-for-You
Why-Doing-It-Yourself-is-Best
Why-Java-Will-Drive-Your-Visitors-Away
Why-Word-is-Bad-for-the-Web
Why-You-Should-Put-Your-Content-in-a-Weblog-Format
Why-You-Should-Stick-to-Design-Conventions
Working-With-Templates
Writing-for-the-Web
Site Map