使用decodeURIComponent的问题

时间:2012-07-11 07:00:00

标签: javascript html decodeuricomponent

我正在尝试解码URL链接中的%20,这样当我将代码从当前页面解码到下一页时,我无法在文本字段中添加%20,下面是我编码解码%20和我放在哪里

function loadRecord(i){
    var item=dataset.item(i);
    fname.value = item['fname'];
    id.value = item['id'];
    window.location.href = decodeURIComponent("userup2.html?id="+item['id']+"&fname="+item['fname']+"&val=300");
    return false;
}

这是当前的并将重定向到第二页以显示数据

enter image description here

enter image description here

为什么它仍会显示%20,即使我编码decodeURIComponent?请帮忙

2 个答案:

答案 0 :(得分:1)

当URI传递给您的输入时,您必须对其进行解码。对其进行解码以将其传递给window.location是正确的,但浏览器将再次将其转义。

我假设您从参数中获取输入值。因此,在您设置params的输入值的情况下(在您的情况下,当然在文档userup2.html中,您必须解码URI。

因此,在您设置params的输入值时,您必须再次对其进行解码:

document.getElementByTagName('input').value = decodeURI(...your code to get the params);

答案 1 :(得分:0)

  

为什么它仍然显示%20,即使我编码decodeURIComponent?

浏览器正在对网址进行编码,使其成为有效的网址。即使你正在解码它,它再次分配给window.location.href,所以控件返回浏览器。

你无法绕过它。而是解码接收端的URL。