目标:正确地将data
属性的字符串放入window.location.hash
。正确编码的字符串在推入window.location.hash时被解码
代码:
map = {path: $(this).attr('data-path'), rev: $(this).attr('data-rev')};
window.location.hash = getMapParams(map);
function getMapParams(map) {
s="";
for(key in map) {
value=eval("map."+key);
if (s.length > 0) {
s+="&";
}
s+=encodeURIComponent(key)+"="+encodeURIComponent(value);
}
return s;
}
问题:只要data-path
属性包含空格的Firefox不能正确地把哈希值。该空格将显示为未编码,而在其他浏览器中,该空格正确编码为%20
。
奇怪的怪癖:如果我调试的代码字符串列表与编码的空间。
研究完成:我发现了很多正确的解决方案阅读在Firefox中的散列。以这样或那样的方式,这对我的代码工作正常。
问题:如何阻止Firefox的从urldecoding我把window.location.hash
从理论上讲这是非常合乎逻辑的Firefox不处理空间中的散列,因为它应该是指一个'ID =“”'并为您滚动页面。你不能使用'_'或其他东西吗? – 2012-03-27 11:22:22
可能不是答案,只需尝试使用value = map [key];并且最后一次返回encodeURIComponent(s)。 – 2012-03-27 11:37:20
我可以用另一个字符替换所有空格,但它是使用Dropbox API获取文件夹和文件的前端。我将不得不改变很多代码。而在其他浏览器中,它工作正常。事情是,这不是一个空间。这是一个urlencoded空间。其他类似'/'('%2F')的urlencoded字符保留原样。 – Wilgert 2012-03-27 11:38:31