2016-01-25 80 views
0

我想我的网址,从这个位置重定向:JavaScript网址重定向时存在URL路径的哈希

http://example.com/#!foo 

到这个位置

http://example.com/folder/#!foo 

如果删除重定向工作如果的条件,但我想重定向我的网址 只有当#url路径中。

我一直在尝试下面的代码为最后30分钟没有任何成功。

<script> 
if(window.location.hash) 

{ 
window.location.assign("http://www.example.com/folder/"+window.location.hash) 
} 
</script> 
+0

第三次魅力 –

+0

的代码,你已经证明工作完全正常,你在测试什么浏览器?请确保在新窗口中进行测试。无论哪种方式有替代选项回答:) –

回答

2

if(window.location.href.indexOf('#') > -1) 
 

 
{ 
 
window.location.assign("http://www.example.com/folder/"+window.location.hash) 
 
}

2

这应该做的。

var address = window.location.href 
 
if (adderss.indexOf('#') > -1){ window.location.assign("http://www.example.com/folder/"+window.location.hash) }

1

尝试:

var url = window.location.href + ""; 
    var str=""; 
    if(url.lastIndexOf("#") != -1) 
    str = url.substring(url.lastIndexOf("#"),url.length); 
    window.location.href = "http://www.example.com/folder/"+str; 

希望它能帮助,干杯!:)

1

您可以使用indexOf来检查字符串中是否存在特定的字符(或字符串)。

<script> 
    if (window.location.hash.indexOf('#') >= 0) { 
     window.location.assign("http://www.example.com/folder/"+window.location.hash) 
    } 
</script> 
1

我会跟下面的事件尝试,

1

你为什么不尝试的IndexOf

if((window.location.pathname).indexOf('#') > 0) 

{ 
    window.location.assign("http://www.example.com/folder/"+window.location.hash) 
}