当前位置:网站首页>关于url编解码应该选用的函数
关于url编解码应该选用的函数
2022-07-27 00:14:00 【Wuuconix】
关于url编解码应该选用的函数
先给出所有的函数
- escape
- unescape
- encodeURI
- decodeURI
- encodeURIComponent
- decodeURIComponent
其中第一组已经被废弃了。因为它不支持unicode的编解码。

可以看到escape对于中文无法获得正确的url编码。
至于 encodeURI 和 encodeURIComponent之间的差别,前者适用于编码整个形成整个URI,而后者适合编码局部,比如search部分。

可以看到encodeURI能够获得一个非常正常的可以访问的URI链接。
而encodeURIComponent对于同样的参数的结果如下。

可以看到,它把正常URI链接里面的 /和#等字符也给编码了,这就导致得到的字符串不再是一个合法的URI链接。
所以encodeURIComponent适合于编码 search部分。比如这样的情景。

这里模拟了file字段里有#和/这样的特殊字符,如果用encodeURI就无法正常编码了,因为它会跳过,而encodeURIComponent则可以完成这一任务。

对于前端来说,一个项目肯定是有一个baseURI的,比如部署在 url.wuuconix.link 。这时候一般来说处理的时候就是往这个baseURI后面加上一个写段,即search。所以encodeURIComponent的使用频率会更大一些。
边栏推荐
- 红宝书第四版的一个错误?
- Heisei thousand words (Heisei, September 10, 2012) (Shidu Mingzuo) the universe is far away, the Milky way is permanent, the sun and moon are running disorderly, the earth is open, the seasons shift,
- 想要彻底搞的性能优化,得先从底层逻辑开始了解~
- 手动从0搭建ABP框架-ABP官方完整解决方案和手动搭建简化解决方案实践
- Tabbar of customized wechat applet on uni app
- 小程序怎样助力智能家居生态新模式
- Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
- ArduinoUNO驱动RGB模块全彩效果示例
- Getlocation:fail the API need to be declared in the requiredprivateinfo field in app.json
- Non global function of lua function
猜你喜欢

Sort icons with swiper

【Redis】五种常用的数据类型

CS224W fall 1.2 Applications of Graph ML

"Software testing" packaging resume directly improves the pass rate from these points

Okaleido Tiger 7.27日登录Binance NFT,首轮已获不俗成绩

Greed - 376. Swing sequence

Concept of data asset management

对象创建的流程分析

Static keyword

Web3.0 world knowledge system sharing - what is Web3.0
随机推荐
什么是进程?
CS224W fall 1.2 Applications of Graph ML
小程序怎样助力智能家居生态新模式
Favicon web page collection icon online production PHP website source code /ico image online generation / support multiple image format conversion
解决小程序报错getLocation:fail the api need to be declared in the requiredPrivateInfos field in app.json
ansible系列之:不收集主机信息 gather_facts: False
iNFTnews | “流量+体验”白衬e数字时装节引领数字时装新变迁
Applet utils
人们为什么热衷于给事物排序
Rust Web(一)—— 自建TCP Server
Okaleido Tiger 7.27日登录Binance NFT,首轮已获不俗成绩
Manually build ABP framework from 0 -abp official complete solution and manually build simplified solution practice
C language program compilation
CS224W fall 课程 ---- 1.1 why Graphs ?
哪家券商开户买REITs基金比较安全?
Kubeadmin到底做了什么?
素因子分解--C(gcc)--PTA
F8 catch traffic, F9 catch rabbits, f10turttle
进程的调度
OD-Paper【3】:Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks