encodeURI与encodeURIComponent

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


参考:http://stackoverflow.com/questions/75980/best-practice-escape-or-encodeuri-encodeuricomponent

http://stackoverflow.com/questions/747641/what-is-the-difference-between-decodeuricomponent-and-decodeuri

http://www.cnblogs.com/s1ihome/archive/2008/05/06/1184254.html

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值