我想在主域和/the/rest/of/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 ?的正确方法是什么?
发布于 2013-06-04 01:05:01
试一试
window.location = location.protocol + '//' + location.host + '/#' + location.pathname如果你想改变显示的url,你可以使用推送状态,例如
history.pushState({}, "page x", location.protocol + '//' + location.host + '/#' + location.pathname);请参阅https://developer.mozilla.org/en-US/docs/Web/Guide/DOM/Manipulating_the_browser_history
发布于 2013-06-04 01:30:41
查看此FIDDLE
代码如下所示:
var a = document.createElement('a');
a.href = location.href;
var path = a.pathname;
a.pathname = "";
a.hash = path;
var resultUrl = a.href;适用于我当前版本的IE、FireFox和Chrome。(IE在兼容模式下是IE10,所以它认为它是8。)
发布于 2013-06-04 01:12:39
这对我来说很有效:
URL = window.location.protocol + '//' + window.location.host + '/#' + window.location.pathname;https://stackoverflow.com/questions/16901981
复制相似问题