当前位置:网站首页>js URLEncode函数
js URLEncode函数
2022-07-25 15:00:00 【Henry_楠】
完美的js URLEncode函数
当需要通过查询字符串传值给服务器时需要对get参数进行encode。
- escape()函数,不会encode
@*/+(不推荐使用) - encodeURI()函数,不会encode
[email protected]#$&*()=:/,;?+'(不推荐使用) - encodeURIComponent()函数,不会encode
~!*()这个函数是最常用的
我们需要对encodeURIComponent函数,最一点修改:
function urlencode (str) {
str = (str + '').toString();
return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}
PS:例如微信H5支付中要求对回调页面的参数redirect_url参数进行 URLEncode。
前端路由采用了hash模式,由于我采用的是encodeURI,导致 # 未 encode而引起#后面内容的丢失,采用encodeURIComponent就好了
边栏推荐
- API health status self inspection
- 深入:微任务与宏任务
- 6线SPI传输模式探索
- Kibana operation es
- "How to use" decorator mode
- Share a department design method that avoids recursion
- How many ways can you assign initial values to a two-dimensional array?
- (原创)自定义一个滚屏的RecyclerView
- 51 single chip microcomputer learning notes (2)
- 6月产品升级观察站
猜你喜欢

bridge-nf-call-ip6tables is an unknown key异常处理

oracle_12505错误解决方法

Nacos2.1.0 cluster construction

51 single chip microcomputer learning notes (2)

Heyuan City launched fire safety themed milk tea to boost fire prevention and control in summer

Leetcode combination sum + pruning

Ssh server rejected password

ESXI6.7.0 升级到7.0U3f(2022年7月12 更新)

Syntax summary of easygui
![[MySQL series] - how much do you know about the index](/img/d7/5045a846580be106e2bf16d7b30581.png)
[MySQL series] - how much do you know about the index
随机推荐
Awk from entry to earth (24) extract the IP of the instruction network card
bridge-nf-call-ip6tables is an unknown key异常处理
Kibana operation es
LeetCode_ String_ Medium_ 151. Reverse the words in the string
Heyuan City launched fire safety themed milk tea to boost fire prevention and control in summer
Overview of cloud security technology development
"How to use" decorator mode
Awk from getting started to digging in (23) awk built-in variables argc, argc -- command line parameter transfer
Process control (Part 1)
6线SPI传输模式探索
Log4j2 basic configuration
[C topic] Li Kou 88. merge two ordered arrays
二维数组赋初值你会几种方法?
Syntax summary of easygui
Visual Studio 2022 查看类关系图
Go language founder leaves Google
spark参数调整调优
[MySQL must know and know] trigger | permission management
C#,C/S升级更新
Live classroom system 05 background management system