在location.pathname之前添加哈希 [英] Add hash before location.pathname

查看:125
本文介绍了在location.pathname之前添加哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在主域和 / / rest / of / url 之间的URL中粘贴哈希值。

I want to stick a hash in URL between the main domain and /the/rest/of/url.

显然,我做得不对。

我用过:

 window.location.hash = location.pathname;

希望更换 http://www.mybusinesssite.com/path/ to / mypage with http://www.mybusinesssite.com/#/path/to/mypage

Hoping to replace http://www.mybusinesssite.com/path/to/mypage with http://www.mybusinesssite.com/#/path/to/mypage

相反,我得到 http://www.mybusinessite.com/path/to/mypage/#/path/to/my/page

什么是正确的方法 http://www.mybusinesssite.com/#/path/to/mypage?

推荐答案

尝试

window.location = location.protocol + '//' + location.host + '/#' + location.pathname

如果你想改变显示的网址,你可以使用推送状态,例如

if you want to change the displayed url you can use push states, e.g.

history.pushState({}, "page x",  location.protocol + '//' + location.host + '/#' + location.pathname);

参见 https://developer.mozilla.org/en-US/docs/Web/Guide/DOM/Manipulating_the_browser_history

这篇关于在location.pathname之前添加哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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