3
從來沒有見過這樣或者可以想象。我的URL看起來與此類似:包含點/期間的window.location.hash的奇怪行爲
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在頁面上。
你有沒有在代碼的其餘部分檢查@ @了window.location的實例?如果你可以在應用程序中重現它,但不能在jsfiddle中重現,那麼問題可能發生在你不期望的地方。 –
令人驚訝的是,只有當散列中出現點時纔會發生這種情況;它始終在沒有點的情況下工作。 – Vad