// URL编码和解码是处理URL中特殊字符的重要方法。
// 在JavaScript中,我们可以使用内置的函数来实现这些功能。
// 1. URL编码 (encodeURIComponent)
// encodeURIComponent 会将字符串中的特殊字符转换为适合在URL中使用的格式。
let originalString = "https://example.com/path?name=John Doe&city=New York";
let encodedString = encodeURIComponent(originalString);
console.log(encodedString);
// 输出: https%3A%2F%2Fexample.com%2Fpath%3Fname%3DJohn%20Doe%26city%3DNew%20York
// 2. URL解码 (decodeURIComponent)
// decodeURIComponent 则会将编码后的字符串还原为原始字符串。
let decodedString = decodeURIComponent(encodedString);
console.log(decodedString);
// 输出: https://example.com/path?name=John Doe&city=New York
// 注意: encodeURIComponent 和 decodeURIComponent 是成对使用的函数,
// 它们可以确保字符串在编码和解码过程中不会丢失信息。
下一篇:js 获取url地址
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站