修改 AJAX 应用程序中的地址栏 URL 以匹配当前状态
我正在编写一个 AJAX 应用程序,但当用户在应用程序中移动时,我希望地址栏中的 URL 能够更新,尽管没有页面重新加载。 基本上,我希望他们能够在任何时候添加书签,从而返回到当前状态。
人们如何在 AJAX 应用程序中维护 RESTful?
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.
How are people handling maintaining RESTfulness in AJAX apps?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
查看 book.cakephp.org 等网站。 该站点更改 URL 时不使用哈希并使用 AJAX。 我不确定它到底是如何做到的,但我一直在努力弄清楚。 如果有人知道,请告诉我。
当查看某个项目内的导航时,也可以访问 github.com。
Look at sites like book.cakephp.org. This site changes the URL without using the hash and use AJAX. I'm not sure how it does it exactly but I've been trying to figure it out. If anyone knows, let me know.
Also github.com when looking at a navigating within a certain project.
检查用户是否“在”页面中,当您单击 URL 栏时,JavaScript 表示您已离开页面。
如果您更改 URL 栏并按其中包含符号“#”的“ENTER”,然后再次进入该页面,而不用鼠标光标手动单击该页面,则来自 JavaScript 的键盘事件命令(document.onkeypress)将能够检查是否已输入并激活 JavaScript 进行重定向。
您可以使用 window.onfocus 检查用户是否在页面中,并使用 window.onblur 检查用户是否在页面外。
是的,这是可能的。
;)
Check if user is 'in' the page, when you click on the URL bar, JavaScript says you are out of page.
If you change the URL bar and press 'ENTER' with the symbol '#' within it then you go into the page again, without click on the page manually with mouse cursor, then a keyboard event command (document.onkeypress) from JavaScript will be able to check if it's enter and active the JavaScript for redirection.
You can check if user is IN the page with window.onfocus and check if he's out with window.onblur.
Yeah, it's possible.
;)
实现此目的的方法是,当 AJAX 更新导致状态更改(您希望拥有离散 URL)时,操作
location.hash
。 例如,如果您的页面网址是:如果客户端函数执行此代码:
则浏览器中显示的 URL将更新为:
这允许用户为页面的“foo”状态添加书签,并使用浏览器历史记录在状态之间导航。
有了这种机制,您就需要使用 JavaScript 在客户端解析 URL 的哈希部分,以创建并显示适当的初始状态,因为片段标识符(# 后面的部分)不会发送到服务器。
如果您使用 jQuery,Ben Alman 的 hashchange 插件 使后者变得轻而易举。
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 discrete URL. For example, if your page's url is:If a client side function executed this code:
Then, the URL displayed in the browser would be updated to:
This allows users to bookmark the "foo" state of the page, and use the browser history to navigate between states.
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.
Ben Alman's hashchange plugin makes the latter a breeze if you're using jQuery.
这和凯文说的很相似。 您可以将客户端状态作为某个 javascript 对象,当您想要保存状态时,可以序列化该对象(使用 JSON 和 base64 编码)。 然后您可以将 href 的片段设置为此字符串。
第一种方法会将新状态视为新位置(因此后退按钮会将它们带到之前的位置)。 后者则不然。
This is similar to what Kevin said. You can have your client state as some javascript object, and when you want to save the state, you serialize the object (using JSON and base64 encoding). You can then set the fragment of the href to this string.
The first way will treat the new state as a new location (so the back button will take them to the previous location). The latter does not.
在使用 Ajax 时,作者不太可能想要重新加载或重定向他的访问者。
但为什么不使用 HTML5 的
pushState
/replaceState
呢?您可以根据需要修改地址栏。 使用 AJAX 获取看起来自然的网址。
查看我最新项目的代码:
http://iesus.se/
It is unlikely the writer wants to reload or redirect his visitor when using Ajax.
But why not use HTML5's
pushState
/replaceState
?You'll be able to modify the addressbar as much as you like. Get natural looking urls, with AJAX.
Check out the code on my latest project:
http://iesus.se/
如果 OP 或其他人仍在寻找修改浏览器历史记录以启用状态的方法,那么按照 IESUS 的建议,使用 PushState 和 ReplaceState 是现在的“正确”方法。 它相对于 location.hash 的主要优点似乎是它创建实际的 URL,而不仅仅是哈希值。 如果保存使用哈希值的浏览器历史记录,然后在禁用 JavaScript 的情况下重新访问,则应用程序将无法运行,因为哈希值不会发送到服务器。 但是,如果使用了 PushState,则整个路由将发送到服务器,然后您可以构建该服务器以对路由做出适当的响应。 我看到一个示例,其中在服务器和客户端上都使用了相同的胡子模板。 如果客户端启用了 JavaScript,他将通过避免与服务器的往返来获得快速响应,但应用程序在没有 JavaScript 的情况下也可以完美运行。 因此,应用程序可以在没有 JavaScript 的情况下正常降级。
另外,我相信有一些框架,其名称如history.js。 对于支持 HTML5 的浏览器,它使用 PushState,但如果浏览器不支持,它会自动回退到使用哈希。
If OP or others are still looking for a way to do modify browser history to enable state, using pushState and replaceState, as suggested by IESUS, is the 'right' way to do it now. It's main advantage over location.hash seems to be that it creates actual URLs, not just hashes. If browser history using hashes is saved, and then revisited with JavaScript disabled, the app won't work, since the hashes aren't sent to the server. However, if pushState has been used, the entire route will be sent to the server, which you can then build to respond appropriately to the routes. I saw an example where the same mustache templates were used on both the server and the client side. If the client had JavaScript enabled, he would get snappy responses by avoiding the roundtrip to the server, but the app would work perfectly fine without the JavaScript. Thus, the app can gracefully degrade in the absence of JavaScript.
Also, I believe there is some framework out there, with a name like history.js. For browsers that support HTML5, it uses pushState, but if the browser doesn't support that, it automatically falls back to using hashes.
window.location.hash 方法是首选的处理方式。 有关如何操作的说明,
Ajax 模式 - 唯一 URL。
YUI 将此模式作为一个模块实现,其中包括 IE 特定的解决方法,用于使后退按钮与使用散列重写地址一起工作。 YUI 浏览器历史记录管理器。
其他框架也有类似的实现。 重要的一点是,如果您希望历史记录与重写地址一起工作,不同的浏览器需要不同的处理方式。 (这在第一篇链接文章中有详细介绍。)
IE 需要一个基于 iframe 的 hack,Firefox 将使用相同的方法生成双重历史记录。
The window.location.hash method is the preferred way of doing things. For an explanation of how to do it,
Ajax Patterns - Unique URLs.
YUI has an implementation of this pattern as a module, which includes IE specific work arounds for getting the back button working along with re-writing the address using the hash. YUI Browser History Manager.
Other frameworks have similar implementations as well. The important point is if you want the history to work along with the re-writing the address, the different browsers need different ways of handling it. (This is detailed in the first link article.)
IE needs an iframe based hack, where Firefox will produce double history using the same method.
SWFAddress 在 Flash 和 Flash 中工作 Javascript 项目允许您创建可添加书签的 URL(使用上面提到的哈希方法)并为您提供后退按钮支持。
http://www.asual.com/swfaddress/
SWFAddress works in Flash & Javascript projects and lets you create bookmarkable URLs (using the hash method mentioned above) as well as giving you back-button support.
http://www.asual.com/swfaddress/