The AJAX Solution
AJAX stands for Asynchronus Javascript And XML. AJAX allows pages to make requests from the server and receive results without leaving the current page. Using Javascript (or JQuery specifically), we can take those results and update only the parts of the page that need it. So the new process is that the web page takes the user input and sends its requests to the server by way of JavaScript. The server responds to the request, as it always has, except it returns only the requested information and not the entire page. Since JavaScript is making the request, it also receives the results. Then, JavaScript is used to update/redraw the parts of the page that need to change. The Asyncrhonus part means that all of this actions happens in the background and does not stop users from continuing to interact with the page in the mean time.
The JQuery .ajax Method
JQuery has a built in .ajax method to perform these asynchronus requests and receive the responses. The general format for the method call is as follows:
$.ajax({
option: value,
option: value,
option: value
});
There are a number of options available in the .ajax method, but there are a few key items that are required to work with XML data. They are as follows:
- type - specifies the type of request. For XML queries, use GET
- url - identifies the file to send the request to. This is typically your XML page.
- dataType - the type of information being requested. For XML, use "xml". For html, use "html".
- success - identifies a function to be run following a successful query. Typically, the success callback function takes a data parameter which is the results of the query.
- error - identifies a function to be run following an unsuccessful query.
JQuery, AJAX and HTML for Including HTML
One of the cheif complaints of web designers is the inefficiency of re-writing the same code on each page. Some of this is alleviated through the use of external css and js scripts. Unfortunately, there is a lot of html structure code that is also repetitive (header, navigation, footer, etc.). When working with a server side script such as PHP, this problem can be fixed through the use of includes. The ability, pre-browser, to include other html files. This allows server side programmers to create a file that is just for the navigation and include it on each page. In this way, if a change needs to be made to the navigation, it is made in one place and then propogates to all of the pages that include that file, similar to external css.
A creative use of JQuery and AJAX allows us to query an html document. And, if we don't pick the resulting code apart, it can simply be appended to an existing element, thus creating a client side include process. See the example below.
HTML File (header.html):
Script File (header.js):
$(document).ready(function(){
$.ajax({
type: 'GET',
url: 'header.html',
dataType: 'html',
success: function(result){
$('body').append(result);
}
});//end ajax
});
Actual Page (index.html):
...