[JavaScript] URL encoding and decoding [reprint]

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/lne818/archive/2008/03/12/2172432.aspx

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.

The encoding method in javascript:

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: @ */+

English description: 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 () method: Convert a URI string to an escape string in UTF-8 encoding format. Characters not encoded by this method :! @ # $ & * () = :/;? +'

English description: 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 () 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 :! *()

English description: 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.

Therefore, for a Chinese string, if you do not want to convert the string encoding format to the UTF-8 format (for example, when the charset of the original page and the target page is consistent), you only need to use escape. If your page is gb2312 or another code, and the page that accepts the parameter is UTF-8 code, use encodeuri or encodeuricomponent.

In addition, encodeuri/encodeuricomponent is introduced after javascript1.5, and escape is available in javascript1.0.

English comments: 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 variable coming, you shoshould 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 shoshould be used in most cases when encoding a single component of a URI. this method will encode certain chars that wowould normally be recognized as special chars for Uris so that specified components may be encoded. note that this method does not encode the 'character, as it is a valid character within Uris.
 
1. Encoding Functions
1) encodeuri returns an encoded URI string. URL is the most common Uri;
2) decodeuri decodes an encoded URI string into the original string and returns it;
3) Example: <script language = "JavaScript"> the output result is as follows: encodestr: http://www.amigoxie.com/index.jsp? Name = % E9 % 98% BF % E8 % 9C % 9C % E6 % 9e % 9C decodestr: http://www.amigoxie.com/index.jsp? Name = xind
 
2. Numeric processing functions
1) parseint converts a specified string to an integer in hexadecimal notation. The syntax format is parseint (numstring, [Radix]). The first parameter is the string to be converted, is a value between 2 and 36, used to specify the hexadecimal format used for String Conversion. Example: The output result is as follows: 32: 32; 032: 26; 0x32: 50 is converted into a binary result: 32: Nan; 032: 0; Result of converting 0x32: 0 to octal: 32: 26; 032: 26; Result of converting 0x32: 0 to hexadecimal: 32: 50; 032: 50; 0x32: 50 11001010 after conversion: 2 hexadecimal: 202; 16 hexadecimal: 285216784 8 hexadecimal: 2359816; 10 hexadecimal: 11001010 43abc after conversion: 43; after abc43 conversion: Nan; after ABC conversion: Nan
2) parsefloat method this method converts a string to a corresponding decimal number. Eg. output result: 4.11 5.1 3) isnan method this method is used to check whether the return values of the first two methods are non-numeric. If yes, true is returned. Otherwise, false is returned.

Related Article

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.