// 使用 JavaScript 进行 URL 编码和解码的示例
// 原始字符串
let originalString = "https://example.com/path?name=John Doe&city=New York";
// 使用 encodeURIComponent 对 URL 中的参数进行编码
let encodedUrl = "https://example.com/path?" +
"name=" + encodeURIComponent("John Doe") +
"&city=" + encodeURIComponent("New York");
console.log("Encoded URL: " + encodedUrl);
// 输出: Encoded URL: https://example.com/path?name=John%20Doe&city=New%20York
// 使用 decodeURIComponent 对编码后的 URL 参数进行解码
let decodedName = decodeURIComponent("John%20Doe");
let decodedCity = decodeURIComponent("New%20York");
console.log("Decoded Name: " + decodedName);
// 输出: Decoded Name: John Doe
console.log("Decoded City: " + decodedCity);
// 输出: Decoded City: New York
encodeURIComponent
对 URL 中的参数部分进行编码。decodeURIComponent
将编码后的参数解码回原始字符串。上一篇:js 截取数字
下一篇:js url 解码
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站