Consider the following code:
hashString = window.location.hash.substring(1);
alert('Hash String = '+hashString);
When run with the following hash:
#car=Town%20%26%20Country
the result in Chrome and Safari will be:
car=Town%20%26%20Country
but in Firefox (Mac AND PC) will be:
car=Town & Country
Because I use the same code to parse query and hash params:
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's idiosyncrasy here breaks it: The car param winds up being "Town", no country.
Is there a safe way to parse hash params across browsers, or to fix how Firefox reads them?
NOTE: This issue is limited to Firefox's parsing of HASH params. When running the same test with query strings:
queryString = window.location.search.substring(1);
alert('Query String = '+queryString);
all browsers will show:
car=Town%20%26%20Country