Shawn Mclean
Shawn Mclean

Reputation: 57479

JQuery Ajax with back button

How do I handle navigation with Jquery Ajax?

For eg. I have a home page. 2 ajax link that loads the content of either page1 and page2.

I'd like the url to be: localhost/page1 or localhost/page2. I know this is possible but I do not know the name of this feature.

Coded examples would be nice.

<div id="nav">
    <a href="page1">Page 1</a> | <a href="page2">Page 2</a>
</div>
<div id="Container"></div>

Upvotes: 2

Views: 1209

Answers (2)

tkt986
tkt986

Reputation: 1028

I am just guesing if you are looking for some thiing like this

$("#nav>a").click(function(e){
 $.get($(this).attr("href"),function(data){
  $("#Container").html(data);
 });
 return false;
});

Upvotes: 0

hunter
hunter

Reputation: 63562

You can change the window.location value to reflect your page changes.

Once you know your page you can do something like this:

var location = window.location.toString();
var pageHash = "#" + page;

if (window.location.hash == "") {
    window.location = location + pageHash;
}
else {
    window.location = location.substring(0, location.indexOf("#")) + pageHash;
}

Upvotes: 2

Related Questions