473,480 Members | 1,847 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to implement an Ajax script?

7 New Member
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/
Jul 3 '09 #1
3 1683
acoder
16,027 Recognized Expert Moderator MVP
That should be all that's required. Can you post the HTML code for the page?
Jul 4 '09 #2
BobBlock
7 New Member
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!
Jul 4 '09 #3
acoder
16,027 Recognized Expert Moderator MVP
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.
Jul 5 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
1534
by: Gaffar | last post by:
Hello. My name is Gaffar. I am handling an module in a Project. This application interface is slow and inefficient. So Client told "implement an AJAX Solution which will increase speed and...
21
6084
by: javainfo | last post by:
How can i refresh IFRAME and load data through AJAX?
1
16468
by: www.web20developers.com | last post by:
http://www.web20developers.com http://www.web20developers.com/index.php?option=com_content&task=view... Ajallerix : AJAX, simple, fast Web image gallery demo ; at Novell AJAX -...
3
2953
by: noballack | last post by:
I've got a problem, I'm working with Ajax in a web with a form with a list of checkbox added to the form via an Ajax.Updater method. These added checkboxs are not been sended by the form if I use...
10
4820
by: trpost | last post by:
I am using ajax / php where I am looking up some info from the database and populating a select list dynamically, however I am running into some sort of size limitation with the ajax.response...
2
4237
by: Nathan Sokalski | last post by:
I am moving my website from my machine to my webhost, and need some help with what extra files I need to include due to the fact that I used AJAX in my site. Everything on the site is obviously...
10
3417
by: paulie | last post by:
Hi, I have been experiencing an issue when trying to use AJAX to reload a DIV area using a timer of 2000ms, which contains a html page with another DIV and javascript. Scenario -------------...
7
4588
by: =?Utf-8?B?QmlsbHkgWmhhbmc=?= | last post by:
我们现在遇到一个问题,通过wcf创建的webservice,选择windows service作为宿主,采用java作为客户端调用成功,但是无法使用asp.net ajax调用。...
1
3256
by: nehalsmistry | last post by:
Hello.. I am using visual studio 2005. So there is framework 2.0 Now I m using ajax toolkit for 2.0 and installed it. But there is no option like Asp.net ajax enable website. But there is 1...
0
7059
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7010
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5362
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
1
4799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4499
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3011
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3003
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.