views:

1274

answers:

1

Right now the biggest issue I'm having with using AJAX is the fact that if I use AJAX on a page, go to another page, then use the browser's back button to go back anything that was changed with AJAX is gone.

I've thought about using the jQuery Addresss plugin to solve that problem but I don't like how it only amends the URL with "#whatever.html" instead of changing it completely.

Ideally, what I would like is to have the URL go from: "www.mysite.com/p:2/" to "www.mysite.com/p:3/" when I make the relevant AJAX call.

Is this at all possible?

+1  A: 

Nope, this is not possible.

I hope you realize that you are trying to address an extremely difficult problem.

Let's say your url looks like

http://mysite.com/mypage/

If you change the window location programmatically to

http://mysite/mypage/1/

Browser will take over and try to navigate to that page, there goes your fancy ajax code!

So what's the alternative? You use URL fragment.

Let's say you have a URL like this,

http://mysite.com/anotherpage/#section

Browser will first load http://mysite.com/anotherpage/ and try to find an anchor named 'section' and scroll to that location. This behavior is exploited by the 'Addresses' plugin. This is similar to how those 'Scroll To Top' links work.

So if you are on the page

http://mysite.com/mypage/

and change the URL to

http://mysite.com/mypage/#1

Browser will not load new page but rather try to find anchor named '1' and scroll to that anchor.

Even if you have managed to add fragments to the URL, it doesn't mean the work is done. If the user presses the back button, DOM will be reset and you will have to parse those fragments and recreate the DOM. It's definitely non-trivial.

SolutionYogi