Home  >  Article  >  Web Front-end  >  JS encoding/decoding analysis of URL strings

JS encoding/decoding analysis of URL strings

高洛峰
高洛峰Original
2017-02-04 09:33:031282browse

Although the three methods escape(), encodeURI(), and encodeURIComponent() can filter some special
characters that affect the integrity of the URL. However, the latter two convert strings into UTF-8 for transmission, which solves the problem of garbled characters caused by inconsistent page encodings
. For example: the encoding format (Charset) of the sending page and the receiving page are inconsistent (assuming that the sending page is GB2312 and the receiving page encoding is
UTF-8), using escape() to convert and transmit Chinese strings will cause garbled characters.
The following are various methods for encoding/decoding URLs under JS:
escape method: Returns an encoded String object that can be read on all computers.
function escape(charString : String) : String
Characters that will not be encoded by this method: @ * / +
Description: The escape method returns a string value (Unicode format) containing the content of charstring. All spaces, punctuation,
accents, and any other non-ASCII characters are replaced with the %xx encoding, where xx is equal to the hexadecimal number representing the character.
For example, spaces are returned as "%20". (Characters with values ​​greater than 255 are stored in %uxxxx format.)
Note: The escape method cannot be used to encode Uniform Resource Identifiers (URIs). To encode it, use the encodeURI and
encodeURIComponent methods.
encodeURI method: Returns a string encoded as a valid Uniform Resource Identifier (URI).
function encodeURI(URIString : String) : String
Characters that will not be encoded by this method: ! @ # $ & * ( ) = : / ; ? + '
Description: The encodeURI method returns an encoded URI. If the encoding result is passed to decodeURI, the initial
string will be returned. encodeURI does not encode the following characters: ":", "/", ";", and "?". Please use
encodeURIComponent to encode these characters.
encodeURIComponent method: Returns a string encoding a valid component as a Uniform Resource Identifier (URI).
function encodeURIComponent(encodedURIString: String): String
Characters that will not be encoded by this method:! * () '
Description: The encodeURIComponent method returns an encoded URI. If the encoding result is passed to
decodeURIComponent, the original string will be returned. Because the encodeURIComponent method will encode all characters,
Please note that if the string represents a path, such as /folder1/folder2/default.html, the slashes in it will also be
encoded, so when the string The string will be invalid when sent as a request to the Web server. If the string contains multiple URI
components, use the encodeURI method to encode.
unescape method: Returns the decoded string from the String object encoded with the escape method.
function unescape(charString : String) : String
Description: The unescape method returns a string value containing the content of charstring. All
characters encoded in %xx hexadecimal form are replaced with equivalent characters in the ASCII character set. (Characters encoded in %uxxxx format (Unicode characters) are replaced with Unicode characters in hexadecimal encoding xxxx.)
Note: The unescape method should not be used to decode Uniform Resource Identifiers (URIs). Please use the decodeURI and
decodeURIComponent methods instead.
decodeURI method: Returns an unencoded form of an encoded Uniform Resource Identifier (URI).
function decodeURI(URIstring : String) : String
decodeURIComponent method: Returns the unencoded form of an encoded component of the Uniform Resource Identifier (URI).
function decodeURIComponent(encodedURIString: String): String
BTW: Method of encoding URL in C#. . .
Encoding: Server.UrlEncode(string)
Decoding: Server.UrlDecode(string) The first three client-side encodings can be decoded in the background using this method.

For more articles related to JS encoding/decoding analysis of URL strings, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn