从Javascript更改URL,如Facebook做
我目前工作在一个面向AJAX的web应用程序。我一直在寻找复制当前AJAX状态的不同方式(或应用程序状态)与浏览器的网址,以便刷新和返回按钮也适用。
I'm currently working in a AJAX oriented web application. I have been looking for the different ways of replicating the current AJAX state (or Application state) with the browsers url so refreshing and back-button also works.
在过去的几个星期我看到涉及使用散列(#)和不同的JS-框架不同的方法。
In the last weeks I see different approaches involving the use of the hash (#) and different JS-frameworks.
在文档中指出,该改变从JS浏览器的URL是不可能的。今天我去Facebook和打开图像和URL变化(Altough图像是在一个收藏夹打开)。后的图像被关闭时,浏览器的URL改回原来的页面。
In the documentation it is stated, that changing the browsers URL from JS is not possible. Today I went to Facebook and opened an image, and the url changed (Altough the image was opened in a lightbox). After the image was closed, the browser url changed back to the original page.
你有什么想法,他们是如何做到这一点的行为呢?
Do you have any idea how they achieve this behavior?
您可以操纵在这样的URL的末尾有值:
You are able to manipulate the has value at the end of the URL like this:
var hashVal = 'somevalue';
window.location.hash = '#' + hashVal;
然后是网址将成为 www.something.com/#somevalue