Connecting Tech Pros Worldwide Help | Site Map

How to implement an Ajax script?

  #1  
Old July 3rd, 2009, 02:52 PM
Newbie
 
Join Date: Jul 2009
Posts: 6
This is a script that, when a link is clicked, will pull a page from somewhere and insert it in a div in the current page. Pretty simple, yes, but being the thick head I seem to be, I can't figure out how to implement it.

i.e. how to formulate the link so that it will point the script to the page we want to load in the div we want?

The script:
Expand|Select|Wrap|Line Numbers
  1. $(document).ready(function() {  
  2.  
  3.     // Check for hash value in URL  
  4.     var hash = window.location.hash.substr(1);  
  5.     var href = $('#nav li a').each(function(){  
  6.         var href = $(this).attr('href');  
  7.         if(hash==href.substr(0,href.length-5)){  
  8.             var toLoad = hash+'.html #content';  
  9.             $('#content').load(toLoad)  
  10.         }   
  11.     });  
  12.  
  13.     $('#nav li a').click(function(){  
  14.  
  15.     var toLoad = $(this).attr('href')+' #content';  
  16.     $('#content').hide('fast',loadContent);  
  17.     $('#load').remove();  
  18.     $('#wrapper').append('<span id="load">LOADING...</span>');  
  19.     $('#load').fadeIn('normal');  
  20.     window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);  
  21.     function loadContent() {  
  22.         $('#content').load(toLoad,'',showNewContent())  
  23.     }  
  24.     function showNewContent() {  
  25.         $('#content').show('normal',hideLoader());  
  26.     }  
  27.     function hideLoader() {  
  28.         $('#load').fadeOut('normal');  
  29.     }  
  30.     return false;  
  31.  
  32.     });  
  33. });
  34.  

The instructions specify the following:

1. We want to target the links within the navigation menu and run a function when they are clicked:
Expand|Select|Wrap|Line Numbers
  1. $('#nav li a').click(function() {  
  2.     // function here  
  3. });
  4.  

2. We need to define what page to get the data from when a link is clicked on:

Expand|Select|Wrap|Line Numbers
  1. var toLoad = $(this).attr('href')+' #content';

3. The loadContent function calls the requested page:

Expand|Select|Wrap|Line Numbers
  1. function loadContent() {  
  2.     $('#content').load(toLoad,'',showNewContent)  
  3. }  

It's very likely that the above is all that's needed to run the script as intended but only if you know how to do it, which I don't.

I will be much obliged if the experts here care to give me a guiding hand.

PS: The tutorial all this comes from is here:
http://net.tutsplus.com/javascript-a...t-with-jquery/
  #2  
Old July 4th, 2009, 11:06 AM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,517
Provided Answers: 12

re: How to implement an Ajax script?


That should be all that's required. Can you post the HTML code for the page?
  #3  
Old July 4th, 2009, 01:58 PM
Newbie
 
Join Date: Jul 2009
Posts: 6

re: How to implement an Ajax script?


acoder,
Thank you for responding.

The HTML code for the page would be meaningless because there is as yet no link associated with the script in it.

The reason being I haven't been able to figure out how to construct such a link; I don't know what to include, in what form and order, etc.

Can you give me a working link example that will point the script to the page we want to load, in the div we want? I suspect that will put me on the right track.

Cheers!
  #4  
Old July 5th, 2009, 03:40 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,517
Provided Answers: 12

re: How to implement an Ajax script?


The script is unobtrusive, i.e. it provides the effect, but if someone doesn't have JavaScript enabled, it will still work. There's nothing special about the links. They're just normal links. See the demo - check the source code.
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cross-Domain / DOM using AJAX & Perl/CGI distraction answers 0 February 11th, 2008 11:34 PM
looking for ajax image upload script Ken1 answers 3 June 27th, 2007 11:25 AM
AJAX.NET and prototype.js Tony answers 31 August 8th, 2006 09:15 PM
Best way to implement extremely heavy calculation? roygon answers 10 April 7th, 2006 04:35 PM