JavaScript code/decode the URL string

Source: Internet
Author: User

Although escape (), encodeuri (), and encodeuricomponent () methods can affect the URL integrity
Character filtering. But the latter two is to convert the string to the UTF-8 way to transfer, solve the problem caused by inconsistent page Encoding
Question. For example, the encoding format of the sending page and the receiving page is inconsistent (assume that the sending page is gb2312 and the receiving page is
UTF-8), the use of escape () conversion to transmit Chinese strings will be garbled.
The following are various methods for URL encoding/Decoding in JS:
Escape method: returns an encoded String object that can be read on all computers.
Function escape (charstring: string): String
Characters not encoded by this method: @ */+
Note: The escape method returns a string value containing charstring content (in unicode format ). All spaces, punctuation marks,
The accent and any other non-ASCII characters are replaced by % XX encoding, where xx equals to the hexadecimal number of the character.
For example, a space is returned as "% 20 ". (Characters with a character value greater than 255 are stored in % uxxxx format .)
Note: The escape method cannot be used to encode the Uniform Resource Identifier (URI. Encodeuri and
Encodeuricomponent method.
Encodeuri: returns a string encoded as a valid Uniform Resource Identifier (URI.
Function encodeuri (uristring: string): String
Characters not encoded by this method :! @ # $ & * () = :/;? +'
Description: The encodeuri method returns an encoded Uri. If the encoding result is passed to decodeuri, the initial
String. Encodeuri does not encode the following characters: ":", "/", ";", and "?". Please use
Encodeuricomponent encodes these characters.
Encodeuricomponent: returns the string of a valid component encoded as a uniform resource identifier (URI.
Function encodeuricomponent (encodeduristring: string): String
Characters not encoded by this method :! *()'
Note: The encodeuricomponent method returns an encoded Uri. If you pass the encoding result
Decodeuricomponent returns the initial string. Because the encodeuricomponent method will encode all characters,
Note that if the string represents a path, for example,/folder1/folder2/default.html, the Slash will also be
Encoding, so that when the string is sent to the Web server as a request, it will be invalid. If the string contains multiple Uris
Component, use the encodeuri Method for encoding.
Unescape method: return the decoded string from the string object encoded using the escape method.
Function Unescape (charstring: string): String
Note: The Unescape method returns a string value containing charstring content. All encoded in % XX hexadecimal format
The characters are replaced by equivalent characters in the ASCII character set. The characters encoded in % uxxxx format (UNICODE character) are sixteen characters.
The Unicode Character of the hexadecimal encoded XXXX is replaced .)
Note: The Unescape method should not be used to decode the Uniform Resource Identifier (URI ). Use decodeuri and
Decodeuricomponent method.
Decodeuri method: returns a non-encoded Uniform Resource Identifier (URI.
Function decodeuri (uristring: string): String
Decodeuricomponent method: return the non-encoding form of an encoded component of the Uniform Resource Identifier (URI.
Function decodeuricomponent (encodeduristring: string): String
The URL encoding method in BTW: C...
Encoding: Server. urlencode (string)
Decoding: Server. urldecode (string) can be used to decode the first three client codes in the background.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.