开发者

What are my options for changing the querystring on a URL, and updating browser history?

Is there any way I can change the URL or add more history to the "back button" without having to refresh the entire page?

My application is AJAX based and I'd like to add some "undo" events to history so that the user can simply hit back and retain the old values.

What's possible today? I hear some o开发者_如何学JAVAf this may be in HTML5 but haven't checked whats supported in current browsers.


I think you can use window.location.hash to track the #part of the page, in your case, #state1, #state2 and so on.

window.location.hash = '#state' + (++ stateN) to set and stateN = parseInt(window.location.hash.match(/\d+$/)[0])

See On - window.location.hash - Change? for more details about how to detect location hash changes.


You could use 301 redirection. Personally, I would use cookies on the client side, or sessions in your back end, to store the breadcrumbs. Storing state information in the URL is a bad idea for AJAX applications, because people might return to a url that the server side is not in the right state to respond to.

Another option would be to provide your own Back button that knows which page to go back to.


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, two projects that you'll probably want to look at which explain the whole hashchange process and using it with ajax are:

  • 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).


It is possible to use ASP.NET's built in Script Manager to update the browser's history. A full how-to to do this is located here:

http://www.asp.net/aspnet-in-net-35-sp1/videos/introduction-to-aspnet-ajax-history

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜