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

相關文章
相關標籤/搜索