Ole Spaarmann
Ole Spaarmann

Reputation: 16749

Handle browser back button with Javascript / different approach possible by using anchors?

I'm currently struggling with a good navigation on a website using Ajax calls and unobstrusive JS. I'm catching the click event on links, load the content, attach it to a div and then return false. This works quite well and also allows Google to crawl the site with speaky URLs.

But I didn't know how to deal with the browser back button. I found this solution to catch the event when the user clicks on the back button:

http://www.bajb.net/2010/02/browser-back-button-detection/

It works quite well. But I also want the back button to work normally when the user found the website via a link and wants to return to the previous page (I don't want to trap anyone).

When I thought about it the best way would be to use anchors. The normal back button supports them and you can go back in history without reloading the page (/#1 <- /#2 <- /#3 etc.)

It would work like this:

Only the last part isn't really clear for me and I could need help here.

Finaly, my question: Does this make any sense?

Thanks!

Upvotes: 3

Views: 7010

Answers (2)

Ole Spaarmann
Ole Spaarmann

Reputation: 16749

I solved the problem by using this great jQuery Plugin: History.js

Thanks!

Upvotes: 1

Marcel
Marcel

Reputation: 28087

Just add the href to window.location.hash after loading the content into a div. Then you can use that back button detection script to load what ever is in the hash.

Upvotes: 1

Related Questions