encodeURI与encodeURIComponent

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

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

一、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. app

encodeURIComponent会把; / ? : @ & = + $ , #等url中的分隔符也编码,这样会致使url不可用,因此encodeURIComponent不要用于对整个url编码,通常用于url中的组成部分编码,好比protocol、hostname、path、query string 函数

二、encodeURI() this

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. url

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

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

四、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

相关文章
相关标签/搜索