gpt4 book ai didi

javascript - 在 location.pathname 之前添加哈希值

转载 作者:行者123 更新时间:2023-11-28 16:01:17 29 4
gpt4 key购买 nike

我想在主域和 /the/rest/of/url 之间的 URL 中添加哈希值。

显然,我做得不对。

我用过:

 window.location.hash = location.pathname;

希望将 http://www.mybusinesssite.com/path/to/mypage 替换为 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

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

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

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

关于javascript - 在 location.pathname 之前添加哈希值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16901981/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com