Jac*_*ack 0 html javascript url
在这种情况下,我想解释问题,因为场景将是解释它的最佳方式.
我在页面中有一个搜索框,A.html传递给此页面的参数应替换为其搜索框的值.问题是,当我传递参数时,空格会被替换,%2520因此错误的值将被添加到搜索框中.我需要解决它.
<a href="www.example.com/a.html?value=Here and there">Link</a>
以下地址将被放入地址栏: www.example.com/a.html?value=Here%2520and%2520there
该值将替换为搜索框的值:Here%2520and%2520there.我需要"Here and There"在搜索框中输入此值.(没有%2520)
这里似乎发生的是URL被双重编码(见下文); 虽然我无法解释为什么会发生这种情况,但可能是因为您的网址未正确进行网址编码:
<a href="www.example.com/a.html?value=Here and there">Link</a>
Run Code Online (Sandbox Code Playgroud)
它应该是:
<a href="www.example.com/a.html?value=Here+and+there">Link</a>
Run Code Online (Sandbox Code Playgroud)
要么:
<a href="www.example.com/a.html?value=Here%20and%20there">Link</a>
Run Code Online (Sandbox Code Playgroud)
双重编码是这样的:
" " regular space
"%20" percent encoded, " " -> "%20"
"%2520" percent encoded, "%" -> "%25"
Run Code Online (Sandbox Code Playgroud)
我无法解释双重编码的原因是因为问题没有确切地说明传递的值是如何添加到搜索框的.最可能的情况是搜索框中填充了百分比编码值.要解决这个问题,您必须首先解码该值,即
searchBox.value = decodeURIComponent('Here%20and%20there');
Run Code Online (Sandbox Code Playgroud)
也可以看看: decodeURIComponent()
| 归档时间: |
|
| 查看次数: |
1326 次 |
| 最近记录: |