从Javascript更改URL,如Facebook做 [英] Change URL from Javascript like facebook does

查看:143
本文介绍了从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

这篇关于从Javascript更改URL,如Facebook做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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