In using JavaScript to encode URL strings, although the three methods escape(), encodeURI(), and encodeURIComponent() can filter some special characters that affect the integrity of the URL.
But the latter two convert strings into UTF-8 for transmission, which solves the problem of garbled characters caused by inconsistent page encoding.
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:
1. 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, a space is returned as "%20". (Character values greater than 255 are stored in %uxxxx format.)
Note: The escape method cannot be used to encode Uniform Resource Identifiers (URIs). It should be encoded using the encodeURI and encodeURIComponent methods.
2. 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 original string will be returned. encodeURI does not encode the following characters: ":", "/", ";", and "?". Please use
encodeURIComponent to encode these characters.
3. encodeURIComponent method: Returns a string of valid components encoded 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 initial 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 that when the string is used as a request It will be invalid when sent to the Web server. If the string contains multiple URI components, use the encodeURI method to encode them.
4. unescape method: Return 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 contents 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.
5. 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 a Uniform Resource Identifier (URI).
function decodeURIComponent(encodedURIString : String) : String
BTW: Method of encoding URL in C#. . .
Encoding: Server.UrlEncode(string)
Decoding: Server.UrlDecode(string) All the previous three client-side encodings can be decoded in the background using this method.