开发者

Javascript window.location search for "#" never returns true

开发者 https://www.devze.com 2022-12-18 09:25 出处:网络
I\'m trying to set up a re开发者_StackOverflow社区director so that when my AJAX functions change the hash part of the URI, the link is still directly accessible should it be copied and pasted.My curre

I'm trying to set up a re开发者_StackOverflow社区director so that when my AJAX functions change the hash part of the URI, the link is still directly accessible should it be copied and pasted. My current function is below; however, it always returns false!

    //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];
    }

How can I change the code to make this work? Is there a better way of doing this? Thanks for your help.

Code updated to:

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

}

Which worked.


Try using window.location.hash directly ;)

0

精彩评论

暂无评论...
验证码 换一张
取 消