Treating Ajax as an enhancement.
By applying this enhancement progressively, graceful degradation is assured.
Treating Ajax as an "all or nothing" technology.
It will take a lot of time and effort to create a "separate but equal" non-Ajax version.
XMLHttpRequest
instead of the server.Back-end architecture should be modular.
Web pages are created by joining modules together (e.g. navigation, log-in form, shopping cart, etc.)
function prepareForm(formId) {
if (!document.getElementById) return false;
if (!document.getElementById(formId)) return false;
document.getElementById(formId).onsubmit = function() {
var data = "";
for (var i=0; i<this.elements.length; i++) {
data+= this.elements[i].name;
data+= "=";
data+= escape(this.elements[i].value);
data+= "&";
}
return (!sendData(data,"formlogic.php"));
};
}
function sendData(data,file) {
var request = getHTTPObject();
if (request) {
request.onreadystatechange = function() {
doSomething(request);
};
request.open( "POST", file, true );
request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
request.send(data);
return true;
} else {
return false;
}
}
A page that includes a form: when the form is submitted, the same page is returned with just part of the page updated.
Example: Contact form (JS)
function prepareLinks(containerId) {
if (!document.getElementById) return false;
if (!document.getElementById(containerId)) return false;
var links = document.getElementById(containerId).getElementsByTagName("a");
for (var i=0; i<links.length; i++) {
links[i].onclick = = function() {
var data = this.getAttribute("href").split("?")[1];
return (!sendData(data,"formlogic.php"));
};
}
}
Clicking on a link returns the same page but with one portion changed (different data or different view of data).
Example: Shopping cart (JS)
Example: Sorting tables (JS)
Ajax problems challenges