0

我查看了各种帖子,现在使用一个函数从 url 检索值,但无法摆脱 %20 个字符。发送的数据是 json,这里是在 url 中发送它的代码:

'<a href="' + url +"?id="+ json_data[i].product_id + 
...
"&description="+ json_data[i].description + '"> //includes white spaces

在另一页上我有:

function getUrlVars()
{
 var vars = [], hash;
 var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
 for(var i = 0; i < hashes.length; i++)
 {
    hash = hashes[i].split('=');
    vars.push(hash[0]);
    vars[hash[0]] = hash[1];
 }
return vars;
}

var selectedData=getUrlVars();
document.write(selectedData.description); 
4

1 回答 1

0

使用encodeURIComponentdecodeURIComponent函数:

"&description="+ encodeURIComponent(json_data[i].description) + '"

和:

decoreURIComponent(hash[1]);

这将照顾空间,但如果&描述中有标志,也会使事情正常进行。
例如:

> hash
["clean", "Machine%20washable%2030%20degrees%20reduced%20spin"]
> decodeURIComponent(hash[1]);
"Machine washable 30 degrees reduced spin"
于 2013-04-16T12:31:18.780 回答