包含点/周期的window.location.hash的奇怪行为

时间:2012-10-24 14:25:38

标签: javascript html5 url hash history.js

从未见过或无法想象。我的网址与此类似:

 www.site.com/root/path1/path2/123/some-path-1/page.1.2.html

在某些时候,我使用哈希更新URL,因此动态用户交互将反映URL地址以向朋友发送链接:

 window.location.hash = 'key=SomeValue';

几乎在所有情况下,它都适合我,除非SomeValue包含点:

 window.location.hash = 'key=SomeValueA.B.';

以上网址变为:

 www.site.com/root/path1/path2/123/some-path-1/key=SomeValueA.B.
而不是      www.site.com/root/path1/path2/123/some-path-1/page.1.2.html#key=SomeValueA.B。

我无法想象这里有什么问题,我无法在jsfiddle中重现它。我使用自定义编写的哈希管理器类:

var hashUrlManager = function(){
 var getHash = function(){       
    return (window.location.hash) ? window.location.hash.substring(1) : "";
 };
 return{
    getHash : getHash,
    getParam : function(k){
        return Util.getParameter(k,"?"+getHash());
    },
    setParam : function(k,v){
        var h = getHash();
        var vNow = hashUrlManager.getParam(k);
        if(vNow==""){
            if(window.location.href.indexOf("#")==-1){
                window.location.hash = k+"="+v;
            }else{
                window.location.hash = h+((h=="")?"":"&")+k+"="+v;
            }
        }else if(vNow!==v){
            window.location.hash = window.location.hash.replace(k+"="+vNow,k+"="+v);                
        }
    },
    removeParam : function(k){
        var v = hashUrlManager.getParam(k);
        var s = k+"="+v;
        if(window.location.hash.indexOf("&"+s)!=-1)s="&"+s;
        window.location.hash = window.location.hash.replace(s,"");
    }
 };
}();

hashUrlManager.setParam('key','SomeValueA.B.');

注意:我确实使用History.js,如果这很重要,那就是页面上的jQuery。

1 个答案:

答案 0 :(得分:1)

我遇到过同样的问题。我在http://jsbin.com/aMUdejA/1/edit?html,output上成功复制了它。这是jQuery(1.x和2.x版本)和&的共存。 History.js。

我提交了一个问题https://github.com/browserstate/history.js/issues/363