最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - window.location.hash issue in Firefox - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question asked Sep 7, 2011 at 17:57 YarinYarin 184k155 gold badges410 silver badges524 bronze badges 1
  • 1 As a side node: This is actually a bug in Firefox – user123444555621 Commented Sep 7, 2011 at 18:59
Add a ment  | 

2 Answers 2

Reset to default 7

A workaround is to use

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

Instead of

window.location.hash.substring(1);

May I also suggest a different method (looks simpler to understand IMHO)

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;
}

Test Case

url = http://stackoverflow./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"}

window.location.toString().split('#')[1] will work in most cases but not if the hash contains another hash (encoded or otherwise).

In other words split('#') might return an array of length>2. Try the following (or own variation) instead:

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)

  1. 暂无评论