Window.location.hash problem in Firefox

Consider the following code:

hashString = window.location.hash.substring(1);
alert('Hash String = '+hashString);

When launched with the following hash:

#car = city% 20% 26% 20Country

the result in Chrome and Safari will be:

car = City% 20% 26% 20Country

but in Firefox (Mac and PC) it will be:

car = city and country

Since I use the same code to parse queries and hash parameters:

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 breaks it down here: “Vehicle parameter” becomes “city”, not a single country.

Is there a safe way to parse hash parameters in browsers or to fix how Firefox reads them?


.. Firefox HASH. :

queryString = window.location.search.substring(1);
alert('Query String = '+queryString);

:

= %20% 26 %20Country

+5
2

window.location.toString().split('#')[1] // car=Town%20%26%20Country

window.location.hash.substring(1);

( )

function getHashParams() {
   // Also remove the query string
   var hash = window.location.toString().split(/[#?]/)[1];
   var parts = hash.split(/[=&]/);
   var hashObject = {};
   for (var i = 0; i < parts.length; i+=2) {
     hashObject[decodeURIComponent(parts[i])] = decodeURIComponent(parts[i+1]);
   }
   return hashObject;
}

url = http://stackoverflow.com/questions/7338373/window-location-hash-issue-in-firefox#car%20type=Town%20%26%20Country&car color=red?qs1=two&qs2=anything

getHashParams() // returns {"car type": "Town & Country", "car color": "red"}
+7

window.location.toString().split('#')[1] , ( ).

, split('#') > 2. ( ):

var url = location.href;        // the href is unaffected by the Firefox bug
var idx = url.indexOf('#');     // get the first indexOf '#'
if (idx >= 0) {                 // '#' character is found
    hash = url.substring(idx, url.length); //the window.hash is the remainder
} else {
    return;                     // no hash is found... do something sensible
}
0

All Articles