修改地址栏的网址在AJAX应用程序,以符合当前国家 [英] Modify Address Bar URL in AJAX App to Match Current State

查看:122
本文介绍了修改地址栏的网址在AJAX应用程序,以符合当前国家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个AJAX应用程序,但用户通过移动应用程序,我想在地址栏中的URL,尽管缺乏页面重新加载更新。基本上,我想为他们能够书签在任何点,并由此返回到当前状态。

I'm writing an AJAX app, but as the user moves through the app, I'd like the URL in the address bar to update despite the lack of page reloads. Basically, I'd like for them to be able to bookmark at any point and thereby return to the current state.

人们如何处理保持RESTfulness在AJAX应用程序?

How are people handling maintaining RESTfulness in AJAX apps?

推荐答案

要做到这一点的方法是操纵的location.hash 在AJAX更新导致状态改变的你想有一个不显眼的URL。举例来说,如果你的页面的网址是:

The way to do this is to manipulate location.hash when AJAX updates result in a state change that you'd like to have a discreet URL. For example, if your page's url is:

http://example.com/

如果一个客户端的功能执行此code:

If a client side function executed this code:

// AJAX code to display the "foo" state goes here.

location.hash = 'foo';

然后,在浏览器中显示的URL将被更新为:

Then, the URL displayed in the browser would be updated to:

http://example.com/#foo

这允许用户书签的页面的富的状态,并使用浏览器的历史记录状态之间切换。

This allows users to bookmark the "foo" state of the page, and use the browser history to navigate between states.

通过这种机制的地方,你就那么需要解析出使用JavaScript来创建并显示相应的初始状态的客户端的URL的哈希部分,作为片段标识符(#之后的部分)不发送到服务器。

With this mechanism in place, you'll then need to parse out the hash portion of the URL on the client side using JavaScript to create and display the appropriate initial state, as fragment identifiers (the part after the #) are not sent to the server.

本Alman的hashchange插件使得后者一件轻而易举的,如果你使用jQuery。

Ben Alman's hashchange plugin makes the latter a breeze if you're using jQuery.

这篇关于修改地址栏的网址在AJAX应用程序,以符合当前国家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆