URL编码是将URL中的特殊字符转换为十六进制编码的过程,而URL解码是将十六进制编码的字符转换回原始字符的过程。在JavaScript中,可以使用encodeURIComponent()decodeURIComponent()函数来进行URL编码和解码。

JS URL编码

在JavaScript中,可以使用encodeURIComponent()函数对URL进行编码。

var url = "http://tool.12333si.com/urlencode/";
var encodedUrl = encodeURIComponent(url);
console.log(encodedUrl);
结果为:​​​​​​​http%3A%2F%2Ftool.12333si.com%2Furlencode%2F

JS URL解码

在JavaScript中,可以使用decodeURIComponent()函数对URL进行解码。

var url = "http%3A%2F%2Ftool.12333si.com%2Furlencode%2F";
var encodedUrl = encodeURIComponent(url);
console.log(encodedUrl);
结果为:http://tool.12333si.com/urlencode/