window.history.pushState到localhost / MySite

时间:2012-06-23 17:03:25

标签: javascript html5

使用pushState更改为URL,以便在点击我网站上的图片时操作浏览器地址栏。

在处理开发环境时如何将其更改回主页。 例如:我的地址栏有此网址: http:// localhost / MySite 点击图片后我将地址栏更改为: http:// localhost / MySite / pic / 123 使用window.history.pushState。

问题是 - 在开发环境中,我无法使用

window.history.pushState(obj, title , '/');

因为斜杠('/')将我带到 http:// localhost /

我尝试了“../”,但它仍然无效 和window.history.back()都没有。

任何建议?

1 个答案:

答案 0 :(得分:1)

使用location.pathname和正则表达式或其他东西来获取基本路径

var viewingImagePattern = /\/pic\/\d+$/;
var rootPath = location.pathname.replace(viewingImagePattern, "/");
window.history.pushState(obj, title , rootPath);
相关问题