天天看點

javascript URL編碼方法的比較(escape encodeURI encodeURIComponent)

javascript中存在幾種對URL字元串進行編碼的方法:escape(),encodeURI(),以及encodeURIComponent() 。這幾種編碼所起的作用各不相同。

escape() 方法:

采用ISO Latin字元集對指定的字元串進行編碼。所有的空格符、标點符号、特殊字元以及其他非ASCII字元 都将被轉化成%xx格式的字元編碼(xx等于該字元在字元集表裡面的編碼的16進制數字 )。比如,空格符對應的編碼是%20。

英文解釋:MSDN JScript Reference: The escape method returns a string value (in Unicode format) that contains the contents of [the argument]. All spaces, punctuation, accented characters, and any other non-ASCII characters are replaced with %xx encoding, where xx is equivalent to the hexadecimal number representing the character. For example, a space is returned as "%20."

Edge Core Javascript Guide: The escape and unescape functions let you encode and decode strings. The escape function returns the hexadecimal encoding of an argument in the ISO Latin character set. The unescape function returns the ASCII string for the specified hexadecimal encoding value.

針對非ASCII字元

第一步  就是你是某種字元(gbk/utf-8),

第二部  将這種字元的(gbk/utf-8)得到unicode 的  16進制的位元組碼

第三部  将這些位元組碼進行url編碼

不會被此方法編碼的字元: @ * / +

encodeURI() 方法:

把URI字元串采用UTF-8編碼格式轉化成escape格式的字元串。

不會被此方法編碼的字元:! @ # $& * ( ) = : / ; ? + '

英文解釋:MSDN JScript Reference: The encodeURI method returns an encoded URI. If you pass the result to decodeURI, the original string is returned. The encodeURI method does not encode the following characters: ":", "/", ";", and "?". Use encodeURIComponent to encode these characters. Edge Core Javascript Guide: Encodes a Uniform Resource Identifier (URI) by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character

針對非ASCII字元

第一步  就是你是某種字元(gbk/utf-8),

第二部  将這種字元的(gbk/utf-8)得到utf-8的  16進制的位元組碼

第三部  将這些位元組碼進行url編碼

encodeURIComponent() 方法:

把 URI字元串采用UTF-8編碼格式轉化成escape格式的字元串。 與encodeURI()相比,這個方法将對更多的字元進行編碼,比如 / 等字元。是以如果字元串裡面包含了URI的幾個部分的話 ,不能用這個方法來進行編碼,否則 / 字元被編碼之後URL将顯示錯誤。

針對非ASCII字元

第一步  就是你是某種字元(gbk/utf-8),

第二部  将這種字元的(gbk/utf-8)得到utf-8的  16進制的位元組碼

第三部  将這些位元組碼進行url編碼

不會被此方法編碼的字元:! * ( ) '

英文解釋:MSDN JScript Reference: The encodeURIComponent method returns an encoded URI. If you pass the result to decodeURIComponent, the original string is returned. Because the encodeURIComponent method encodes all characters, be careful if the string represents a path such as /folder1/folder2/default.html. The slash characters will be encoded and will not be valid if sent as a request to a web server. Use the encodeURI method if the string contains more than a single URI component. Mozilla Developer Core Javascript Guide: Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, or three escape sequences representing the UTF-8 encoding of the character.

因 此,對于中文字元串來說,如果不希望把字元串編碼格式轉化成UTF-8格式的(比如原頁面和目标頁面的charset是一緻的時候),隻需要使用 escape 。如果你的頁面是GB2312或者其他的編碼,而接受參數的頁面是UTF-8編碼的,就要采用encodeURI或者 encodeURIComponent。

另外,encodeURI/encodeURIComponent是在javascript1.5之後引進的,escape則在javascript1.0版本就有。

英文注釋:The escape() method does not encode the + character which is interpreted as a space on the server side as well as generated by forms with spaces in their fields. Due to this shortcoming, you should avoid use of escape() whenever possible. The best alternative is usually encodeURIComponent().Use of the encodeURI() method is a bit more specialized than escape() in that it encodes for URIs [REF] as opposed to the querystring, which is part of a URL. Use this method when you need to encode a string to be used for any resource that uses URIs and needs certain characters to remain un-encoded. Note that this method does not encode the ' character, as it is a valid character within URIs.Lastly, the encodeURIComponent() method should be used in most cases when encoding a single component of a URI. This method will encode certain chars that would normally be recognized as special chars for URIs so that many components may be included. Note that this method does not encode the ' character, as it is a valid character within URIs.

繼續閱讀