最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AJAX and back button - Stack Overflow

programmeradmin2浏览0评论

How can I save history with AJAX just how github or google+ do. I use jQuery and I do not want to use some kind of hack like the # and #! tricks, because if I do, I'll need to change my whole project and make my life much more plicated?

How can I save history with AJAX just how github or google+ do. I use jQuery and I do not want to use some kind of hack like the # and #! tricks, because if I do, I'll need to change my whole project and make my life much more plicated?

Share Improve this question asked Aug 24, 2011 at 11:43 NikolayNikolay 891 silver badge3 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

Github and Google+ are using history.pushState.

You can change the current url like this:

history.pushState(null, null, '/the-new-url');

This is supported by Firefox, Chrome, Opera, Safari; not IE.

See https://developer.mozilla/en/DOM/Manipulating_the_browser_history#The_pushState%28%29.c2.a0method

There are basically two options when it es to AJAX and history.

  1. The # method (which isn't a hack btw). Since you don't want that one you only have option 2 left.

  2. Use PushState. This is what Facebook, GitHub and a few others use. It's not supported by all browsers though and if you want plete cross-browser patibility you will have to use a system which can degrade to option 1.

A very simple way to implement this is Backbone.js Router class.

There is a simple jQuery hashchange plugin that is designed to solve this specific problem.

I haven't used it; I don't know how it keeps track of the url history.

发布评论

评论列表(0)

  1. 暂无评论