#&q=car&category=Car%20Audio%2CAccessories&brand=我借用了上一个问题中的this函数,因此:
function insertParam(key, value)
{
key = escape(key); value = escape(value);
var kvp = document.location.hash.substr(1).split('&');
var i=kvp.length; var x; while(i--)
{
x = kvp[i].split('=');
if (x[0]==key)
{
x[1] = value;
kvp[i] = x.join('=');
break;
}
}
if(i<0) {kvp[kvp.length] = [key,value].join('=');}
//this will reload the page, it's likely better to store this until finished
document.location.hash = kvp.join('&');
}我是这样使用它的:
insertParam("category",xy);
insertParam("brand",zy);我的问题是它正在将逗号解码为%2C。我知道我可以处理服务器端的字符,但是我怎样才能用javascript让它看起来更漂亮呢?我的意思是用逗号替换%2c。
发布于 2012-02-06 03:52:05
我不知道为何在先前的答案中被划掉了,但答案是正确的。
alert(decodeURIComponent('%2C'));因此,您将查询字符串拆分为元素,并按&符号进行拆分。然后通过= symbol拆分结果,并对名称和值应用decodeURIComponent。
ps:key = escape(key); value = escape(value);你不应该在这里使用转义(它在不同的浏览器上是不同的。我所说的“不同”指的是IE)。使用encodeURIComponent。
pps:because they either encode commas or don't encode &=??
alert(encodeURIComponent('&=,'));输出%26%3D%2C
发布于 2014-06-11 23:34:09
这对我在包含逗号的URI上撤消encodeURIComponent()很有效:
.replace(/%2C/g,",")https://stackoverflow.com/questions/9151566
复制相似问题