【问题标题】:Decode value from URL using Javascript [duplicate]使用Javascript从URL解码值[重复]
【发布时间】:2016-09-08 14:53:27
【问题描述】:

我从 URL 中获取一个参数值,如下所示:

users%2F3

应该是用户/3

如何解码使用 Javascript 获得的值?

我之前尝试过以下方法:

var a = "users%2F3"; 
console.log(decodeURI(a));

但日志显示相同的编码值。

【问题讨论】:

标签: javascript


【解决方案1】:

以富有想象力的命名decodeURIComponent function。 (另见the specification)。

【讨论】:

  • 我之前试过如下: var a = "users%2F3"; console.log(decodeURI(a));并且日志显示相同的值...
  • @MiguelMoura 是的,完全不同的函数 (decodeURI) 与 decodeURIComponent 的作用不同。
  • 啊现在我明白我做错了什么。谢谢。
【解决方案2】:

解码网址的代码 html code

<!DOCTYPE html>
<html>
<body>

<p>Click the button to decode a URI after encoding it.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
var uri = "users%2F3";
var uri_dec = decodeURIComponent(uri);
var res = "Decoded URI: " + uri_dec;
document.getElementById("demo").innerHTML = res;
}
 </script>

</body>
</html>

【讨论】:

    【解决方案3】:

    如果你愿意,你可以创建自己的函数,比如

    function urldecode(str) { return decodeURIComponent((str+'').replace(/\+/g, '%20')); };

    这也会将 decodeURI 组件中的“+”替换为空格

    【讨论】:

      【解决方案4】:

      你可以使用

      decodeURIComponent(a)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-07
        • 1970-01-01
        • 2013-08-23
        • 2012-08-16
        • 2014-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多