JavaScript URL encoding conversion function

Source: Internet
Author: User
When url is used for parameter transfer, some parameters or URL addresses with Chinese names are often passed.

When url is used for parameter transfer, some parameters or URL addresses with Chinese names are often passed, and conversion errors may occur during backend processing. In some transfer pages, GB2312 is used, and UTF8 is used on the receiving page. In this way, the received parameters may be different from the original ones. The URL encoded using the urlEncode function on the server is different from the URL encoded using the encodeURI function of the client javascript.

JavaScript Coding involves three functions: escape, encodeURI, encodeURIComponent, and corresponding three decoding functions: unescape, decodeURI, and decodeURIComponent.

Escape () method

The specified string is encoded using the ISO Latin character set. All space characters, punctuation marks, special characters, and other non-ASCII characters will be converted into character encoding in % xx format (xx equals to the hexadecimal encoding of this character in the character set table number ). For example, the space character is encoded as % 20. The unescape method is the opposite. Characters not encoded by this method: @ */+

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.

That is, the escape () function can be used when JavaScript uses data.

When encoding unicode values other than 0-, escape outputs % u *** format. In other cases, escape, encodeURI, and encodeURIComponent have the same encoding result.

EncodeURI () method

Convert a URI string to an escape string in UTF-8 encoding format. Characters not encoded by this method :! @ # $ & * () = :/;? +'

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.

EncodeURI () can be used as a whole during url jump, for example:

Location.href=encodeURI("http://www.nowamagic.net/");
EncodeURIComponent () method

Convert a URI string to an escape string in UTF-8 encoding format. Compared with encodeURI (), this method will encode more characters, such. Therefore, if the string contains several parts of the URI, this method cannot be used for encoding. Otherwise, the URL will display an error after the/character is encoded. Characters not encoded by this method :! *()

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.

You must use encodeURIComponent when passing parameters so that the combined url will not be truncated by special characters such. For example:

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.