preserving browser “back” button functionality using AJAX/jQuery to load pages and history.pushState() method

对着背影说爱祢 提交于 2019-12-02 23:32:09
tim peterson

I'd like to direct everyone to a jQuery plugin called PJAX which accomplishes what I was ultimately interested in asking this question. Pjax() combines jQuery AJAX and pushState for page loading and preserving browser history.

Here's the code, and here's a nice demo of pjax() in action.

It is really great and easy to use, in the demo, just remember to click the checkbox which is there to help demonstrate pjax()'s functionality.

For this you need to subscribe to the 'popstate' event.

The popstate event is fired in certain cases when navigating to a session history entry. HTML5 spec

You could do this like this:

window.onpopstate = function() {
  $('#main_content').load(location.href)
};

You can use the popstate event for this purpose. See https://developer.mozilla.org/en/DOM/window.onpopstate for details.

Also have a look at History.js which provides a wrapper and can fallback to url hashtags in case the browser does not support the history api.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!