jQuery snippet to Ajaxify your Website

This is a short tutorial using AJAX. I know that this may be a little old, or that maybe somebody has posted this before, but I wanted to show how easy it is “ajaxify” an entire website with just a few lines of code. What we can assume is that we have main a container that changes on every page.

(function($){
   $(document).ready(function(){
       var containerId = '#container',
       $container = $(containerId);
       $('a').live('click', function(e){
              $container.load($(this).attr('href') + '  ' + containerId);
              e.preventDefault();
        });
   });
})(jQuery);

What the previous lines of code do is get the element that changes on the page, then add the click event in all the links. This click event brings the page via AJAX and filters only the content of the requested page. The downside of this approach is that the whole page is requested and then filtered by the jQuery selector; however, once the page is requested it gets cached by the browser.

Teylor Feliz
Teylor Feliz

Teylor is a seasoned generalist that enjoys learning new things. He has over 20 years of experience wearing different hats that include software engineer, UX designer, full-stack developer, web designer, data analyst, database administrator, and others. He is the founder of Haketi, a small firm that provides services in design, development, and consulting.

Over the last ten years, he has taught hundreds of students at an undergraduate and graduate levels. He loves teaching and mentoring new designers and developers to navigate the rapid changing field of UX design and engineering.

Articles: 186