博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
encodeURI与encodeURIComponent
阅读量:7083 次
发布时间:2019-06-28

本文共 2882 字,大约阅读时间需要 9 分钟。

  hot3.png

js对url进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,

相应3个解码函数:unescape,decodeURI,decodeURIComponent

1、encodeURIComponent

encodeURIComponent() is a global function that returns an encoded copy of its s argument.

ASCII letters and digits are not encoded, nor are the following ASCII punctuation characters:
- _ . ! ~ * ' ( )
All other characters, including punctuation characters such as /, :, and # that serve to separate the various components of a URI, are replaced with one or more hexadecimal escape sequences.
Note the difference between encodeURIComponent() and encodeURI(): encodeURIComponent() assumes that its argument is a portion (such as the protocol, hostname, path, or query string) of a URI. Therefore it escapes the punctuation characters that are used to separate the portions of a URI.

encodeURIComponent会把; / ? : @ & = + $ , #等url中的分隔符也编码,这样会导致url不可用,所以encodeURIComponent不要用于对整个url编码,一般用于url中的组成部分编码,比如protocol、hostname、path、query string

2、encodeURI()

encodeURI() is a global function that returns an encoded copy of its uri argument. ASCII letters and digits are not encoded, nor are the following ASCII punctuation characters:

- _ . ! ~ * ' ( )

Because encodeURI() is intended to encode complete URIs, the following ASCII punctuation characters, which have special meaning in URIs, are not escaped either:
; / ? : @ & = + $ , #
Any other characters in uri are replaced by converting each character to its UTF-8 encoding and then encoding each of the resulting one, two, or three bytes with a hexadecimal escape sequence of the form %xx. In this encoding scheme, ASCII characters are replaced with a single %xx escape, characters with encodings between \u0080 and \u07ff are replaced with two escape sequences, and all other 16-bit Unicode characters are replaced with three escape sequences.
If you use this method to encode a URI, you should be certain that none of the components of the URI (such as the query string) contain URI separator characters such as ? and #. If the components have to contain these characters, you should encode each component separately with encodeURIComponent().
Use decodeURI() to reverse the encoding applied by this method. Prior to ECMAScript v3,
you can use escape() and unescape() methods (which are now deprecated) to perform a similar kind of encoding and decoding.

encodeURI会保留; / ? : @ & = + $ , #这些字符,如果url中的某些部分带有这些关键字,可能就会出现问题。所以推荐使用encodeURIComponent来对component进行编码,然后各部分连接起来

3、escape已经不推荐使用,前后台对应编码时会有兼容性问题,在某些浏览器会可能会不好用。

4、escape对0-255以外的unicode值进行编码时输出%u****格式,其它情况下escape,encodeURI,encodeURIComponent编码结果相同。

最多使用的应为encodeURIComponent,它是将中文、韩文等特殊字符转换成utf-8格式的url编码,所以如果给后台传递参数需要使用encodeURIComponent时需要后台解码对utf-8支持(form中的编码方式和当前页面编码方式相同)

escape不编码字符有69个:*+-./@_0-9a-zA-Z

encodeURI不编码字符有82个:!#$&'()*+,-./:;=?@_~0-9a-zA-Z

encodeURIComponent不编码字符有71个:!'()*-._~0-9a-zA-Z

参考:

转载于:https://my.oschina.net/caiwf/blog/144168

你可能感兴趣的文章
BZOJ2730:[HNOI2012]矿场搭建——题解
查看>>
分区表转换
查看>>
ASP.NET 一般处理程序 接收文件上传
查看>>
WebGIS前端地图显示之根据地理范围换算出瓦片行列号的原理(核心)(转)
查看>>
Java Excel 列号数字与字母互相转换
查看>>
Android中下拉框spinner的两种用法
查看>>
Hibernate SQL
查看>>
C#中的静态
查看>>
MySQL的语法高级之SELECT
查看>>
Unity3D脚本行尾(Line Endings)
查看>>
Bootstrap3 排版-页面主体
查看>>
【pom.xml 依赖】使用net.sf.json-lib-2.4-jdk15.jar所需要的其他依赖架包 以及其一直在pom.xml报错的问题...
查看>>
Git与GitHub的简单了解(3)
查看>>
NN-Neural Network
查看>>
JMeter 配置元件之-HTTP Cookie管理器-实现 Cookie 登录
查看>>
从length与length()开始谈Java
查看>>
linux下 安装 ImageMagick 及其 php imagick扩展
查看>>
小tip: margin:auto实现绝对定位元素的水平垂直居中
查看>>
colgroup和col的区别
查看>>
在半透明div上面字体也半透明的问题
查看>>