2010-01-30 64 views
2

我试图设置一个重定向器,以便当我的AJAX函数更改URI的哈希部分时,链接仍然可以直接访问,如果它被复制并粘贴。我目前的功能在下面;但是,它总是返回false!Javascript window.location搜索“#”永远不会返回true

//If a hash is found, redirect it 
    var current_uri = String(window.location); 

    if (current_uri.search('/\#/') != -1) { 
      var current_uri_array = current_uri.split('#'); 
      window.location = current_uri[1]; 
    } 

如何更改代码以使其工作?有没有更好的方法来做到这一点? 感谢您的帮助。

代码更新为:

if (window.location.hash) { 
    window.location = window.location.hash.substring(1); 

}

哪些工作。

回答

5

尝试直接使用window.location.hash

+0

我的代码更新为: //如果找到一个哈希值,并将其重定向到 如果(window.location.hash){ 了window.location = window.location的。 hash.substring(1); } 这工作。谢谢。 – ensnare 2010-01-30 23:04:40

相关问题