JavaScript URL encoding and decoding using instructions _javascript tips

Source: Internet
Author: User
Tags numeric numeric value

When you use GB2312 on some delivery pages and use UTF8 on the receiving page, the parameters you receive may be inconsistent with what you originally received. URLs encoded using the server-side UrlEncode function are not the same as URLs encoded using the encodeURI function of client JavaScript.
Encoding methods in javascript:
Escape () method:
The specified string is encoded using the ISO Latin character set. All spaces, punctuation, special characters, and other non-ASCII characters will be converted into%XX-formatted character encodings (XX equals the encoded 16 digits of the character in the character set table). For example, spaces's corresponding encoding is%20. The Unescape method is the opposite. Characters that will not be encoded by this method: @ */+

English explanation: 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 no other non-ascii characters-are with replaced%xx, where xx is equivalent to the hexadecimal number representing the character.
For example, a and 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 the argument in the ISO Latin character set.
The Unescape function returns the ASCII string for the specified hexadecimal encoding value.

encodeURI () method: Converts the URI string into a string in escape format using the UTF-8 encoding format. Characters that will not be encoded by this method:! @ # $& * () =:/;? + '

English explanation: MSDN JScript reference:the encodeURI method returns a encoded URI.
If you are the result of the 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: Converts the URI string into a string in escape format using the UTF-8 encoding format. This method encodes more characters than encodeURI (), such as/or characters. So if a string contains several parts of the URI, this method cannot be encoded, otherwise the URL will display an error after the/character is encoded. Characters that will not be encoded by this method:! * ( )

English explanation: MSDN JScript reference:the encodeURIComponent method returns a encoded URI. If you are the result of the 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 would be encoded and won't be valid if sent as a request to a Web server. Use the encodeURI to 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 Chinese strings, you only need to use escape if you do not want to convert the string encoding format into a UTF-8 format (such as when the original page and the target page charset are consistent). If your page is GB2312 or other code, and the page that accepts the parameter is UTF-8 encoded, you should use encodeURI or encodeuricomponent.
In addition, Encodeuri/encodeuricomponent was introduced after javascript1.5, and escape was in the javascript1.0 version.

English Note: The Escape () method does not encode the + character which is interpreted as a spaces on the server side as OK as ge Nerated by forms and spaces in their fields.
Due to this shortcoming, your should avoid use of escape () whenever possible. The best alternative is usually encodeuricomponent ().
Use of the encodeURI () method was a bit more specialized than escape () in, it encodes for URIs [REF] as opposed to the QueryString,
Which is part of a URL. Use the to encode a string of need to is used for all resource that uses URIs and needs certain characters T O remain un-encoded. Note that this method does not encode the ' character, as it is a valid character within, the uris.lastly NT () method should is used in most
Cases when encoding
A single component of a URI. This method would encode certain chars that would normally is recognized as special chars for URIs May is included.
This is
Does not encode the "character, as it is a valid character within.

1. Code processing function
1) encodeURI Returns a result of encoding the URI string. URLs are the most common kind of URIs;
2) decodeURI decodes a encoded URI string into the original string return;
3) Example: < Script language = "JavaScript" > Output as follows: encodestr:http://www.amigoxie.com/index.jsp?name=%e9%98%bf%e8%9 c%9c%e6%9e%9c Decodestr:http://www.amigoxie.com/index.jsp?name=xind
2. Numerical processing function
1) parseint converts a specified string into an integer, in syntax format: parseint (numstring, [Radix]) The first argument is a string to be converted, a numeric value between 2 and 36 that specifies the system to use for string conversions. Examples are as follows: The output is as follows: Results by default: 32:32;032:26;0X32:50 into 2 results: 32:nan;032:0;0x32:0 Conversion to 8 results: 32:26;032:26;0x32:0 Converted to 16 results: 32:50;032:50;0x32:50 11001010 Conversion Results: 2:202;16:285216784 8 in: 2359816;10:11001010 43abc conversion: 43; Abc43 after conversion: NAN;ABC after conversion: NaN
2 Parsefloat Method This method converts a string to a decimal number. eg. The output is as follows: 4.11 5.1 3) isNaN method This method is used to detect whether the first two methods return values are non-numeric, and if so, return true, otherwise, reverse false

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.