How to keep the browser history in sync when using Ajax?

醉酒当歌 提交于 2019-12-17 06:45:27

问题


I'm writing a simple photo album app using ASP.NET Ajax.
The app uses async Ajax calls to pre-load the next photo in the album, without changing the URL in the browser.

The problem is that when the user clicks the back button in the browser, the app doesn't go back to the previous photo, instead, it navigates to the home page of the application.

Is there a way to trick the browser into adding each Ajax call to the browsing history?


回答1:


Update: There is now the HTML5 History API (pushState, popState) which deprecates the HTML4 hashchange functionality. History.js provides cross-browser compatibility and an optional hashchange fallback for HTML4 browsers.

The answer for this question will be more or less the same as my answers for these questions:

  • How to show Ajax requests in URL?
  • How does Gmail handle back/forward in rich JavaScript?

In summary, you'll definitely want to check out these two projects which explain the whole hashchange process and adding ajax to the mix:

  • jQuery History (using hashes to manage your pages state and bind to changes to update your page).

  • jQuery Ajaxy (ajax extension for jQuery History, to allow for complete ajax websites while being completely unobtrusive and gracefully degradable).




回答2:


MSDN has an article about Managing Browser History in ASP.NET AJAX




回答3:


Many websites make use of a hidden iframe to do this, simply refresh the iframe with the new URL, which adds it to the browsing history. Then all you have to do is handle how your application reacts to those 'back button' events - you'll either need to detect the state/location of the iframe, or refresh the page using that URL.




回答4:


You can use simple & lightweight PathJS lib.

Usage example:

Path.map("#/page1").to(function(){
    ...
});

Path.map("#/page2").to(function(){
    ...
});

Path.root("#/mainpage");
Path.listen();



回答5:


The 3.5 SP1 update has support for browser history and back button in ASP.NET ajax now.




回答6:


For all solutions about the back button, none of them are "automatic". With every single one you are going to have to do some work to persist the state of the page. So no, there isn't a way to "trick" the browser, but there are some great libraries out there that help you with the back button.




回答7:


Info: Ajax Navigation is a regular feature of the upcoming IE8.




回答8:


If you are using Rails, then definitely try Wiselinks https://github.com/igor-alexandrov/wiselinks. It is a a Swiss Army knife for browser state management. Here are some details: http://igor-alexandrov.github.io/blog/2013/07/11/the-way-to-wiselinks-1-dot-0/.



来源:https://stackoverflow.com/questions/29886/how-to-keep-the-browser-history-in-sync-when-using-ajax

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