• Home
  • Services
  • Profile
  • Portfolio
  • Contact
  • Links
  • Blog
  • Archives
  • Sitemap
Subscribe: Posts | Comments | E-mail
  • Ajax
  • Design
  • Development
  • Freebies
  • IT News
  • Miscellaneous
  • Programming
  • Tutorials
  • Web 2.0

Bali Web Design

Posted on September 20, 2008 - by webmaster

Ajaxify your web pages using Jquery

Ajax 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
  • 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, 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.

12 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.



Leave a Comment

Here's your chance to speak.

  1. Name (required)

    Mail (required)

    Website

    Message

  • Ad Ad Ad Ad
  • Recommended Links

  • Featured News

    • PHP Class Create Short URL via TinyURL, Is.gd, Hex.io, Tr.im & Bit.ly API by webmaster on September 24, 2009
    • How to embed plurk status in wordpress blog by ruby on September 12, 2009
    • Website Optimization Best Practices, speed up your website load by ruby on May 21, 2009
    • Prism Firefox Extension, bring your web apps to the desktop by ruby on May 11, 2009
    • Troubleshoots During Migration by webmaster on March 7, 2009
  • Latest Entry

    • PHP Class Create Short URL via TinyURL, Is.gd, Hex.io, Tr.im & Bit.ly API
    • How to embed plurk status in wordpress blog
    • Scripty2 the successor of script.aculo.us
    • Social Network Icon Pack
    • jQuery validation engine, jquery inline form validation
  • Tag Cloud

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

    • { 80 } ResponsesPHP ajax login form using Jquery
    • { 79 } Responsesphp ajax tutorial create ajax based login form using jquery
    • { 57 } ResponsesASP Classic Programming Still Alive
    • { 50 } Responses63+ best practice to optimize PHP code performances
    • { 37 } ResponsesVideobox: Lightbox for videos
© 2007 - 2010 Bali Web Design all rights reserved
Powered by wordpress Theme by woothemes