1

I've been working on a project using the hashbang (#!) method to skip though pages. Basically there is only 1 page, and when you click to go to a diffrent page, you stay on the page, but the URL changes, e.g. from index.html#home to index.html#about and new content is loaded via AJAX/JSON. All works well, but if I go "back" (or forward) in the browser, only the page in the URL changes, but my jQuery isn't fired to reload the content.

What I need is some code that will handle both the back and forward actions in all browsers, so that I can fire the function to load the page from jQuery. How do I do this?

4

3 回答 3

4

为什么要重新发明轮子?History.js是一个出色且维护良好的 jQuery 插件,它支持新的HTML5 History API,并且在不支持 History API 时优雅地回退到使用哈希 URL。适用于几乎所有浏览器(甚至 IE 6)。

于 2011-12-21T19:00:18.053 回答
1

有一个 jQuery-Plugin 似乎正在做你想要的。

http://www.asual.com/jquery/address/

这是询问什么是最好的后退按钮 jQuery 插件的最高投票答案?

于 2011-12-18T23:42:54.607 回答
1

看看这个hashchange事件。它在哈希更改时触发。但是,您应该只为 Internet Explorer 8 支持执行此操作。

Internet Explorer 7 不支持hashchange,因此您不能依赖它。至于 Internet Explorer 9,它(当然还有 Chrome、Safari 和 Firefox)支持History API,你应该使用它。它使您的 URL 保持清晰、简短和语义化,同时支持优雅的后退/前进按钮。

于 2011-12-18T23:32:21.453 回答