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

js url encode编码转换

作者:此岸蝶恋花   发布日期:2025-06-27   浏览:36

// 使用 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

解释说明:

  1. encodeURIComponent: 用于对 URL 中的参数进行编码,确保特殊字符(如空格、&、= 等)被正确处理。
  2. decodeURIComponent: 用于对编码后的 URL 参数进行解码,恢复原始字符。
  3. 示例:
    • 我们首先定义了一个包含特殊字符的 URL。
    • 使用 encodeURIComponent 对 URL 中的参数部分进行编码。
    • 最后使用 decodeURIComponent 将编码后的参数解码回原始字符串。

上一篇:js 截取数字

下一篇:js url 解码

大家都在看

js 数组对象排序

js 数组删掉第一个值

js fill

js 数组复制

js 复制数组

js 数组拷贝

js 对象转数组

js 深拷贝数组

js 获取今天年月日

js jsonp

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

Laravel 中文站