URL編碼轉(zhuǎn)換,escape() encodeURI() encodeURIComponent()
更新時(shí)間:2006年12月27日 00:00:00 作者:
escape() 方法:
采用ISO Latin字符集對指定的字符串進(jìn)行編碼。所有的空格符、標(biāo)點(diǎn)符號、特殊字符以及其他非ASCII字符都將被轉(zhuǎn)化成%xx格式的字符編碼(xx等于該字符在字符集表里面的編碼的16進(jìn)制數(shù)字)。比如,空格符對應(yīng)的編碼是%20。unescape方法與此相反。不會被此方法編碼的字符: @ * / +
英文解釋: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.
encodeURI() 方法:把URI字符串采用UTF-8編碼格式轉(zhuǎn)化成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
encodeURIComponent() 方法:把URI字符串采用UTF-8編碼格式轉(zhuǎn)化成escape格式的字符串。與encodeURI()相比,這個方法將對更多的字符進(jìn)行編碼,比如 / 等字符。所以如果字符串里面包含了URI的幾個部分的話,不能用這個方法來進(jìn)行編碼,否則 / 字符被編碼之后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.
另外,encodeURI/encodeURIComponent是在javascript1.5之后引進(jìn)的,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.
采用ISO Latin字符集對指定的字符串進(jìn)行編碼。所有的空格符、標(biāo)點(diǎn)符號、特殊字符以及其他非ASCII字符都將被轉(zhuǎn)化成%xx格式的字符編碼(xx等于該字符在字符集表里面的編碼的16進(jìn)制數(shù)字)。比如,空格符對應(yīng)的編碼是%20。unescape方法與此相反。不會被此方法編碼的字符: @ * / +
英文解釋: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.
encodeURI() 方法:把URI字符串采用UTF-8編碼格式轉(zhuǎn)化成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
encodeURIComponent() 方法:把URI字符串采用UTF-8編碼格式轉(zhuǎn)化成escape格式的字符串。與encodeURI()相比,這個方法將對更多的字符進(jìn)行編碼,比如 / 等字符。所以如果字符串里面包含了URI的幾個部分的話,不能用這個方法來進(jìn)行編碼,否則 / 字符被編碼之后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.
復(fù)制代碼 代碼如下:
因此,對于中文字符串來說,如果不希望把字符串編碼格式轉(zhuǎn)化成UTF-8格式的(比如原頁面和目標(biāo)頁面的charset是一致的時(shí)候),只需要使用escape。如果你的頁面是GB2312或者其他的編碼,而接受參數(shù)的頁面是UTF-8編碼的,就要采用encodeURI或者encodeURIComponent。
英文注釋: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.
您可能感興趣的文章:
- js中編碼函數(shù):escape,encodeURI與encodeURIComponent詳解
- js中字符串編碼函數(shù)escape()、encodeURI()、encodeURIComponent()區(qū)別詳解
- 談?wù)別ncodeURI和encodeURIComponent以及escape的區(qū)別與應(yīng)用
- 深入分析escape()、encodeURI()、encodeURIComponent()的區(qū)別及示例
- javascript 字符 Escape,encodeURI,encodeURIComponent
- escape、encodeURI 和 encodeURIComponent 的區(qū)別
- escape、encodeURI、encodeURIComponent等方法的區(qū)別比較
- 簡單明了區(qū)分escape、encodeURI和encodeURIComponent
相關(guān)文章
關(guān)于JavaScript對象類型之Array及Object
這篇文章主要介紹了關(guān)于JavaScript對象類型之Array及Object,Array 類型是 ECMAScript 中最常用的類型了。而且,ECMAScript 中的數(shù)組與其他多數(shù)語言中的數(shù)組有著相當(dāng)大的區(qū)別,需要的朋友可以參考下2023-05-05
JavaScript中函數(shù)的常用寫法及調(diào)用方法
這篇文章介紹了JavaScript中函數(shù)的常用寫法及調(diào)用方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
Javascript入門學(xué)習(xí)第五篇 js函數(shù)
上篇文章講了js中對象和數(shù)組的一些方法。 這章我們先說說函數(shù),然后來點(diǎn)實(shí)戰(zhàn)。2008-07-07
JavaScript 表單處理實(shí)現(xiàn)代碼
這篇文章主要介紹了JavaScript 表單處理實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-04-04
js四舍五入數(shù)學(xué)函數(shù)round使用實(shí)例
這篇文章主要介紹了js四舍五入數(shù)學(xué)函數(shù)round使用實(shí)例,需要的朋友可以參考下2014-05-05

