小码哥的IT人生

JavaScript decodeURIComponent() 函数

JavaScript基础 2022-06-08 10:43:49小码哥的IT人生shichen

JavaScript decodeURIComponent() 函数

实例

编码后解码 URI:

var uri = "http://www.phpcodeweb.com/my test.asp?name=ståle&car=saab";
var uri_enc = encodeURIComponent(uri);
var uri_dec = decodeURIComponent(uri_enc);
var res = uri_enc + "<br>" + uri_dec;

完整实例:

<!DOCTYPE html>
<html>
<body>
<p>请单击按钮,在编码 URI 后对其解码。</p>
<button onclick="myFunction()">试一试</button>
<p id="demo"></p>
<script>
function myFunction() {
  var uri = "http://www.phpcodeweb.com/my test.asp?name=ståle&car=saab";
  var uri_enc = encodeURIComponent(uri);
  var uri_dec = decodeURIComponent(uri_enc);
  var res = "Encoded URI: " + uri_enc + "<br>" + "Decoded URI: " + uri_dec;
  document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

定义和用法

decodeURIComponent() 函数对 URI 组件进行解码。

提示:请使用 encodeURIComponent() 函数对 URI 组件进行编码。

浏览器支持

函数 Chrome Edge Firefox Safari Opera
decodeURIComponent() 支持 支持 支持 支持 支持

语法

decodeURIComponent(uri)

参数值

参数 描述
uri 必需。要解码的 URI。

技术细节

返回值: 字符串,表示解码后的 URI。

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024