• Home
  • Services
  • Profile
  • Portfolio
  • Contact
  • Links
  • Blog
  • Archives
  • Sitemap
Subscribe: Posts | Comments | E-mail
  • Ajax XMLHttpRequestAsynchronous HTTP Request How To's
  • FreebiesBest Freebies Collection
  • MiscellaneousNews, Announcements & General Stuffs
  • Print & Web DesignPhotoshop, Vector, CSS, XHTML
  • ProgrammingProgramming PHP, ASP, .NET and more

Bali Web Design

Posted on September 20, 2008 - by webmaster

Ajaxify your web pages using Jquery

Ajax XMLHttpRequest Freebies Tutorials Web 2.0

This simple tutorial example will show you how to dynamically load your web content via ajax instead of default behaviour HTTP request (full round trip), before we continue you may want to have a look at the demo page.

To run this example you need to download jquery library or you can use Google Ajax Libraries API, Next create an HTML page name it “index.html” copy paste the codes below, and change the codes as neccesary:

<html>
<head>
<title>Title</title>
<script type="text/javascript" src="path/to/jquery/jquery.js"></script>
<script type="text/javascript">
// javascript codes goes here
</script>
<style type="text/css">
/* CSS goes here */
/* keep this! */
#loading {display:none;} /*hide the loading element by default*/
</style>
</head>
<body>
<!-- navigation -->
<div id="nav">
<ul>
<!-- current class for the first load page -->
<li><a class="current" href="homepage.html">Home</a></li>
<li><a href="services.html">Services</a></li>
<li><a href="about.html">About me</a></li>
</ul>
</div>
<!-- content holder -->
<div id="wrapper"></div>
</body>
</html>

In this example assumed your website contain of 3 contents homepage, services and about, so here we need to create those pages, simply copy paste the codes below, add the content, and gve a name: homepage.html,about.html, and services.html

<div class="container">
<!-- Content Goes Here ... -->
</div>

No head, no body tags because we already put those tags in index.html and we are simply load these pages into wrapper div at index.html.

Now the main part, we need to write the javascript codes which will handle the ajax request. add these codes to index.html:

//1
$(function () {
//2
function ajaxify(file){
//3
$('<div id="loading"></div>').html("Loading Content: "+file+" ...").appendTo('body').fadeIn();
//4
$.get(file,function(data) {
//5
$("#wrapper").slideUp('slow',function(){
//6
$(this).html(data).slideDown('slow',function(){
//7
$('#loading').fadeOut('slow',function(){$(this).remove();});
});
});
});
}

//8
$("div#nav ul li a").click(function(){
//9
$('#nav ul li a.current').removeClass('current');
//10
$(this).addClass('current');
//11
ajaxify($(this).attr('href'));
//12
return false;
});

//13
ajaxify('homepage.html');

});
  1. The very basic jquery syntax, it tells browser to execute the codes inside the brackets only if the DOM is ready for any modification that going to be made by the scripts
  2. procedure block declaration to handle the onclick method (@ #11 & #13), param (string) the URL to load
  3. create a new div element with class “loading”, add the HTML string into it, then add itself as a child of body, and fade it in (take a look at stylesheet declaration, we set the display attribute to “none”)
  4. create HTTP GET request via ajax, parameter file is the url of the file to load, and on data received …
  5. Slide up the wrapper div and once the slide up effect completed …
  6. Put the data as HTML string into the wrapper div ($(this) refers to wrapper div) then slide it down slowly, and once slide down completed …
  7. Fade out the loading element, and then remove it from the DOM when fade out effect completed
  8. Anchor inside the selector pattern (read the jquery docs about selector) on click …
  9. remove the class current from the element which currently has it
  10. add the current style to the current clicked anchor
  11. execute ajaxify function with parameter the URL (href) of the current clicked anchor
  12. important!, to prevent default behaviour of a link, otherwise it will load the URL with full round trip HTTP request
  13. For the first loaded index.html, tell the browser to load the homepage.html via ajax

!update better approach available here, prevent a fall on screen readers without javascript support also SEO compliants

Share this article:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • MySpace
  • Ping.fm
  • Pownce
  • Slashdot
  • StumbleUpon
  • Technorati
  • Tumblr
  • TwitThis
This entry was posted on Saturday, September 20th, 2008 at 09:49 and is filed under Ajax XMLHttpRequest, Freebies, Tutorials, Web 2.0. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

16 Comments

We'd love to hear yours!



  1. Visit My Website

    September 26, 2008

    Permalink

    ghozan said:

    can we try it with php? or is there other specific requirements? thanks.



  2. Visit My Website

    September 26, 2008

    Permalink

    demonita said:

    sure, you can load HTML generated by any server side codes, just change the href target



  3. Visit My Website

    September 27, 2008

    Permalink

    Marius said:

    How do search engines open the pages? Will they index them as any other static html?



  4. Visit My Website

    September 30, 2008

    Permalink

    Komang said:

    check out the newer post http://www.chazzuka.com/blog/?p=246 about SEO friendly and accessible ajax, if we have some time, we’ll turn this sample into indexable ajax page



  5. Visit My Website

    October 1, 2008

    Permalink

    X said:

    Hi,

    if you doubleclick the tab preloading text will show forever.
    Is there a way to avoid this?

    Thanks



  6. Visit My Website

    October 2, 2008

    Permalink

    Komang said:

    @Mr.X
    change the click handler:

    $(“div#nav ul li a”).click(function() {
    if(!$(this).hasClass(“current”)) {
    .. load
    }
    return false;
    });



  7. Visit My Website

    October 13, 2008

    Permalink

    Christopher said:

    Really this is good information about Ajax.



  8. Visit My Website

    November 11, 2008

    Permalink

    Will said:

    Nice example! I will try to implement this one on my websystem. Thanks!



  9. Visit My Website

    December 31, 2008

    Permalink

    Bassel Khartabil said:

    Thank you very much
    I’ll use your method a lot ;-)



  10. Visit My Website

    April 8, 2009

    Permalink

    JBotte said:

    I can’t seem to find the updated page with the seo friendly version and the no js compatibility.



  11. Visit My Website

    April 13, 2009

    Permalink

    webmaster said:

    the link is updated check it out



  12. Visit My Website

    January 4, 2010

    Permalink

    henri said:

    simple but useful.

    Thank for sharing your knowledge.



  13. Visit My Website

    February 27, 2010

    Permalink

    dwk said:

    great work! just what i need, thanks! God Bless!



  14. Visit My Website

    March 24, 2010

    Permalink

    Ajax Jquery Tutorial: ajaxify your web pages – qweb.ro – webdesign blog said:

    [...] http://www.chazzuka.com/blog/?p=88 Ti-a placut ce ai citit? Aboneza-te prin RSS PHP JAVASCRIPT jquery AJAX script calendar data gratis image plugin SEO WORDPRESS 3d 3gpp adobe analiza analysis analytics api basic camp carti cautare class custom excel fields flash free functii ©2010 qweb.ro – platforma: Wordpress [...]



  15. Visit My Website

    May 2, 2010

    Permalink

    Baker said:

    Thanks for sharing. Great article.



  16. Visit My Website

    May 29, 2010

    Permalink

    Jeff said:

    Great article! Using it on my own site now.

    2 issues I faced though:

    1) The loading div doesn’t seem to work properly – it fades in properly enough, but for some reason it doesn’t fade out at all. The function don’t seem to be even read at all.

    2) Is there any ways you can some how add a anchorURL so users can actually use the back button? Right now the URL remains fixed. An anchor URL tutorial would be so awesome! Like instead of services.html the URL will be #services

    Other than that, many thanks for a great and simple AJAX usage tutorial!

    Jeff



Leave a Comment

Here's your chance to speak.

  1. Name (required)

    Mail (required)

    Website

    Message

  • Most Popular Posts

    • { 108 } ResponsesPHP ajax login form using Jquery
    • { 79 } Responsesphp ajax tutorial create ajax based login form using jquery
    • { 71 } ResponsesASP Classic Programming Still Alive
    • { 53 } Responses63+ best practice to optimize PHP code performances
    • { 44 } ResponsesVideobox: Lightbox for videos
  • Latest Posts

    • PHP Newbie: Proper handling of Error & Exception in PHP
    • Why SEO lost importance
    • jQuery Mobile Announced : Touch-Optimized Web Framework for Smartphones & Tablets
    • Multiple Google Account Signin
    • Install, Configure & Running Memcache in Windows as Service
    • DynamicWP Image Cube Wordpress Plugin
    • New Wordpress 3.0 API menu_page_url
    • Wordpress 3.0 Custom Taxonomy
    • JQuery DataGrid Plugin Compass
    • Upgrading to upcoming CodeIgniter 2.0
  • Categories

    • advertorial
    • Ajax XMLHttpRequest
    • Featured
    • Freebies
    • Miscellaneous
    • Print & Web Design
    • Programming
    • Tutorials
    • Web 2.0
  • Featured News

    • PHP Newbie: Proper handling of Error & Exception in PHP by webmaster on August 18, 2010
    • Why SEO lost importance by webmaster on August 16, 2010
    • jQuery Mobile Announced : Touch-Optimized Web Framework for Smartphones & Tablets by webmaster on August 16, 2010
    • Install, Configure & Running Memcache in Windows as Service by webmaster on June 27, 2010
    • Website Optimization Best Practices, speed up your website load by ruby on May 21, 2009
  • Tag Cloud

    • Actionscript advertorial Ajax XMLHttpRequest API best practices CakePHP chat Classic ASP CMS CSS Development facebook firefox Flash flickr Freebies gmail HTML icons Javascript Jobs jQuery jQuery Plugins Library Lightbox memcache Mootools news Personal PHP PHP Frameworks plurk Print & Web Design prism Prototype regex regular expression Scriptaculous SEO Tutorial twitter Web 2.0 Wordpress Works YUI
  • Archives

    • August 2010
    • June 2010
    • May 2010
    • September 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • October 2007
copyleft 2007 - 2010 Bali Website Design Studio