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 web mail 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.
|
|
Web Design
Home Page
Photoshop: A Graphic Designer's Dream.
Using Quizzes And Games To Get Traffic.
10 Easy Ways To Promote Your Website.
Python And Ruby: The Newer Alternatives.
The Top 10 Biggest Web Design Mistakes.
Building A Budget Website.
How The Web Works.
The Art Of The Logo.
VBScript: Javascript Made Easy.
|
Web Design
5 Important Rules In Website Design
... visitors have a reason to click on the "back" button! Give them the value of your site up front without the splash page. 2) Do not use excessive banner advertisements Even the least net savvy people have trained themselves to ignore banner advertisements so you will be wasting valuable website real estate. ...
The Confusing World Of Web Hosting: Making Your Decision.
... domain from them. This is generally an expensive option, and a bad idea you'll be getting few features compared to what you're paying. Few people who are serious about web hosting get it from the same place they get their domains. So Where Should I Start? Well, that all depends on what your website is ...
Designing For Sales.
... attention to is the headline on your sales page. It needs to be large, to stand out, and to grab the visitors' attention. It should give a clear benefit (not a feature) of your product that you think would appeal to most people. If you have a bad headline then people won't even look at the rest of what ...
Designing For Search Engines.
... your content remains relatively intact. If it doesn't, then you'll be turning search engines away. Finally, Write Great Content. The key with modern search engines (and, at the same time, the thing you have least control over) is how many people decide to link to your page from their page. How can you ...
Finding A Good HTML Editor.
... thing left to think about: which program are you going to use to do it? While you can use programs like Notepad or Wordpad that come with Windows, they don't have any specialised HTML editing features, and that can slow you down more than you'd think. The choice of HTML editors out there, though, is bewildering: ...
| |