Click here to Skip to main content
15,906,624 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

Quick question, i've done plenty of googling and reading and just can't seem to find a simple solution that works. I am using the following jquery code to load the contents of files into my main Div when each of the links on my site is clicked:

JavaScript
$(document).on('ready', function() {

  // click functions
  $("[href='#about']").click(function(){
    $("#main").load("../about.php");
  });
  $("[href='#contact']").click(function(){
    $("#main").load("../contact.php");
  });
  });
});


Everything works great, as expected, but the back button doesnt work. The hash in the address bar changes when I press back, but nothing happens and the content doesnt change. I've seen articles on using jquery-history.js bbq.js and others, but nothing that I've been able to make work.

Can someone give me an idea of how I would need to adapt my code above to make things happen? Much obliged.
Posted
Comments
Sergey Alexandrovich Kryukov 22-Sep-13 12:30pm    
The question is: why do you think "Back" should work? Conceptually, it should work based on the user navigation events, not the formal HTTP requests. Where the user performs some navigation action, so the previous page would be accessible? You did not describe such scenario.

If, by some reason, there is an inconsistency ("Back" button is enabled, but clicking on it does not cause navigation), it's the different story. Maybe, you need to do just the opposite, disable "Back" (clear the cash)?

—SA
bigcdh 22-Sep-13 12:38pm    
Sorry if I wasnt clear. The back button doesnt work because the content has been loaded via an AJAX request. therefore I believe I need to listen or action some kind of hash change event, but thats what i'm struggling to code. Hope i'm making sense..!

1 solution

Sorted this now:

using jquery.address.js

JavaScript
$.address.change(function(event) {
    $('#main').load('../' + event.value + '.php');
});
$('a').click(function() {
    $.address.value($(this).attr('href'));
});


Happy days.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900