Firefox中的window.location.hash问题 [英] window.location.hash issue in Firefox
问题描述
hashString = window.location.hash.substring(1);
alert('Hash String ='+ hashString);
使用以下哈希运行时:
#car =镇%20%26%20Country
和 Safari :
汽车=城镇%20%26%20国家
但是在 Firefox (Mac和PC)中:
car = Town&因为我使用相同的代码来分析查询和散列参数:
pre>function parseParams(paramString){
var params = {};
var e,
a = / \ + / g,//正则表达式用空格替换符号
r = /([^& ;; =] +)=?([^ & ;;] *)/ g,
d = function(s){return decodeURIComponent(s.replace(a,)); },
q = paramString; (e = r.exec(q))
params [d(e [1])] = d(e [2]);
返回参数;在这里,火狐的特性打破了它:汽车的参数被卷起来有没有一种安全的方法来解析浏览器的散列参数,或修复Firefox读取它们的方式?
注意:此问题仅限于Firefox解析HASH参数。当使用查询字符串运行相同的测试时:
queryString = window.location.search.substring(1);
alert('Query String ='+ queryString);
所有浏览器都会显示:
car = Town%20%26%20Country
解决方案是使用
window.location.toString()。split('#')[1] // car = Town% 20%26%20Country
而不是
window.location.hash.substring(1);
我也可以建议一个不同的方法(看起来更简单,理解恕我直言)
function getHashParams(){
//同时移除查询字符串
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]);
}
返回hashObject;
测试用例 b
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()//返回{car type:Town& Country,car color:red}
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
解决方案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.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"}
这篇关于Firefox中的window.location.hash问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!