I deployed in a website an Ajax reload for every pages. This website use a small audio player and the sound was stopped each time a new page was requested. That wasn't so graceful. The result now is exactly the same website reloading the pages through Ajax, without changing accessibility: I didn't modify ehe HTML source.

I did it quickly bu using jQuery. As this method could be useful for others, I will describe step by step how I made it.
The website which I worked on was pretty small: Amandine Café. Nothing better to train himself in a small example and then apply it in big projects

Current Website Analysis

The first thing to do is to navigate in the whole website to deduct the fixed zone and dynamic zone to reload. You have to identify which links will reload which zones, and what will be happen regarding the reloaded page. For instance you have to worry about the possible javascript control which could be a problem. Write down them and we'll take care of that at the end.
This part was pretty fast for me as the website don't use sub navigation and contains only 5 pages. I found 2 zones delimited by 3 div identified with the id header, page, and footer. Only the div#page vary regarding the pages. That will be only this content that I will update with Ajax.

PHP Side Preparation

Before dealing with the javascript code, we'll update the PHP code of our pages. When an Ajax load is made, we won't reload the full page, but only the content to update. For more simplicity and flexibility, we'll use the same URL than the real links. That's mean we have to be able to provide only the requested content in PHP, without the head page, or the fixed div. If you don't want –can't– do that, take a look to the little bonus at the bottom of this post...
The easiest way to do that is to put the fix part in separate files, named randomly header.php and footer.php. These files will be include in every pages; that's may already the case, using the include function. The thing is now to don't show these parts when the request is an Ajax request. Pretty easy, we'll write a PHP function: isAjax. The file includes will occur only if isAjax is false. In others words:
if (!isAjax())
    inlcude('header.php');

The isAjax function is, as you can see, very very hard:
function isAjax() {
    return array_key_exists('HTTP_X_REQUESTED_WITH', $_SERVER);
}
Actually jQuery –also Prototype, I don't know about the others– add a HTTP_X_REQUESTED_WITH element in the request that you can retrieve in the $_SERVER global variable.
We're done with the PHP preparation pages, which are now able to provide light version of your pages especially for the Ajax request.

Javascript Integration

We're moving now on the real Ajax request programmation. To keep the website totally accessible, which will works exactly in the same way without Javascript, We won't modify any current HTML sources. We'll just include jQuery in all the pages and one other javascript file containing the upcoming code.
The script schema will be:
  1. On the page load, intercept all links pointed to .php pages
  2. Add an onclick function to make the Ajax request and stop the page change
  3. On the click, call the page through Ajax with the same URL and then integrate the result where we want.
  4. Stop the click propagation
With jQuery, we're writing that very simply:
$(function() {
    $('a[href*=.php]').click(function() {
       $('#page').load(this.href);
return false;
    });
});
Very short, isn't it?
Of course this code won't work perfectly in every case. In my example I have to update the body id according to the requested page; that's mean the navigation bar link will going brown as expected by the graphic designer. To do so we can use the third parameter of the load function which will be called once the Ajax load is done. Here is how looks like my function:
$(function() {
    $('a[href*=.php]').click(function() {
        $('#page').load(this.href, null, function() {
        $('body').attr('id', this.href.substring(this.href.indexOf('.')));
        });
return false;
    });
});
If you read the code in the live site, that's not at all like that because I occured other problems.

Little Bonus

If you can't–don't want– deploy the PHP cutting for the Ajax request, don't stop right now! Firstly it could be possible to reload the whole page. This solution will be very sad and you'll probably occurs many others problems.
jQuery will help. Indeed, in the URL provided to the load function, you can specifiy a HTML selector to filter the result and show only the part you want to.
In my example the call could become simply:
$('#page').load(this.href+' #page');

I dissuade to use this technique as that's mean you will reload the whole page. So we loose the Ajax utility to reduce data transfer between clients and server.

If it's not that simple?

Give proof of imagination!
As I already thought about this question, I'll give you some example soon. This post is yet consistent and you can start to enjoy. Finally I hope you won't wait me to do that... Or if you have a website where do you want deploy this kind of solution, let me know and I will use it as my example.

Express Yourself!

How did you find this post? Interesting, boring, totally useless, can do better?
Do you have some questions? Suggestions?
Did you read some English mistakes?
Use the comments!

Version française de ce billet