Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

js url encode

作者:惨美歌   发布日期:2026-07-04   浏览:50

// 使用 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("原始 URL: " + originalString);
console.log("编码后的 URL: " + encodedUrl);

// 解码 URL
let decodedUrl = decodeURIComponent(encodedUrl);
console.log("解码后的 URL: " + decodedUrl);

解释说明:

  1. encodeURIComponent:用于对 URL 中的参数值进行编码,确保特殊字符(如空格、&、= 等)被正确转义。它会将这些字符转换为百分号编码的形式(例如,空格会被编码为 %20)。

  2. decodeURIComponent:用于对经过 encodeURIComponent 编码的字符串进行解码,恢复为原始字符串。

  3. 应用场景:当你需要在 URL 中传递包含特殊字符的参数时,使用 encodeURIComponent 可以确保 URL 的正确性和可读性。

上一篇:js urlencode编码

下一篇:js url.createobjecturl

大家都在看

js 数组打乱顺序

js 两个数组取交集

js 数组对象排序

js 对象数组排序

js 数组删掉第一个值

js fill

js fill方法

js 数组连接

js json数组

js 数组复制

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站