// 示例代码:使用 decodeURIComponent 解码 URI 组件
// 编码的 URI 组件字符串
let encodedURI = "https://example.com/path?name=John%20Doe&city=New%20York";
// 使用 decodeURIComponent 解码整个 URI(不推荐)
try {
console.log(decodeURIComponent(encodedURI));
} catch (e) {
console.error("解码失败:", e);
}
// 正确的方式是只解码查询参数中的值
let params = "name=John%20Doe&city=New%20York";
let decodedParams = decodeURIComponent(params);
console.log(decodedParams); // 输出: name=John Doe&city=New York
// 如果你只想解码特定的查询参数,可以这样做:
let name = "John%20Doe";
let city = "New%20York";
console.log(decodeURIComponent(name)); // 输出: John Doe
console.log(decodeURIComponent(city)); // 输出: New York
decodeURIComponent 用于解码由 encodeURIComponent 编码的 URI 组件。decodeURIComponent 解码整个 URL,因为这可能会导致错误(例如,解码 URL 中的特殊字符如 ? 和 &)。上一篇:js 刷新页面
下一篇:js lastindexof
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站