-2

enter code hereThis JavaScript snippet removes the /#/and/everything/after from URL, and reloads the page.

var loc = window.location.href,
    index = loc.indexOf('#');

if (index > 0) {
  window.location = loc.substring(0, index);
}

As a result, something like this:

http://www.somesite.com/#/somepage

reloads as this:

http://www.somesite.com

How can I modify the snippet, to make it remove only this part: #/, but keeo everything that comes after it, and load the page at the url that has everything before and after #/, like this:

http://www.somesite.com/somepage
4

2 回答 2

2

你可以做

window.location = window.location.href.replace(/\/#\//, '/');

如果你只想在有 # 时重新加载,你可以这样做

if (window.location.href.indexOf('/#/')>=0) {
    window.location = window.location.href.replace(/\/#\//, '/');
}

编辑:要在最后一个标记之前添加 # ,您可以这样做:

var href2 = href.replace(/([^\/]+\/?)$/, '#/$1')

例子 :

"http://www.somesite.com/somepage?a=b" -> "http://www.somesite.com/#/somepage?a=b"
"http://www.somesite.com/somepage/" -> "http://www.somesite.com/#/somepage/"
于 2013-06-03T15:58:06.693 回答
0

你可以使用

window.location.href.replace("#/","");

于 2013-06-03T16:34:10.060 回答