解码URIComponent和解码URI有什么区别?

encodeURIComponent

要在JavaScript中解码URL组件,请使用decodeURLComponent()方法。

示例

您可以尝试运行以下代码来解码URL组件-

<!DOCTYPE html>
<html>
   <body>
      <button onclick="display()">Check</button>
      <p id="demo"></p>
      <script>
         function display() {
            var uri = "http://example.com/welcome msg.jsp?name=åmit&sub=programming";

            //首先编码
            var encode = encodeURIComponent(uri);
            var decode = decodeURIComponent(encode);

            var result = "Encode= " + encode + "<br>" + "Decode= " + decode;
            document.getElementById("demo").innerHTML = result;
         }
      </script>
   </body>
</html>

解码URI

要在JavaScript中解码URL,请使用decodeURI()方法。

示例

您可以尝试运行以下代码来解码URL-

<!DOCTYPE html>
<html>
   <body>
      <button onclick="display()">Check</button>
      <p id="demo"></p>
      <script>
         function display() {
            var uri = "welcome msg.jsp?name=åmit&sub=programming";

            //首先编码
            var encode = encodeURI(uri);
            var decode = decodeURI(encode);

            var result = "Encode= " + encode + "<br>" + "Decode= " + decode;
            document.getElementById("demo").innerHTML = result;
         }
      </script>
   </body>
</html>