Consider the following code:
hashString = window.location.hash.substring(1);
alert('Hash String = '+hashString);
当使用以下哈希值时:
在Chrome和Safari中的结果为:#car=Town%20%26%20Country
但是在Firefox(Mac和PC)中的结果为:car=Town%20%26%20Country
因为我使用相同的代码来解析查询和哈希参数。car=Town & Country
function parseParams(paramString) {
var params = {};
var e,
a = /\+/g, // Regex for replacing addition symbol with a space
r = /([^&;=]+)=?([^&;]*)/g,
d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
q = paramString;
while (e = r.exec(q))
params[d(e[1])] = d(e[2]);
return params;
}
Firefox在这里有些奇怪,导致它解析的“car”参数实际是“Town”,而不是国家。
是否存在安全的方法可以跨浏览器解析哈希参数,或者修复Firefox的解析方式?
注意:此问题仅限于Firefox对哈希参数的解析。当使用查询字符串运行相同的测试时:
queryString = window.location.search.substring(1);
alert('Query String = '+queryString);
all browsers will show:
car=Town%20%26%20Country