Home > Web Front-end > JS Tutorial > Instructions for using javascript URL encoding and decoding_javascript skills

Instructions for using javascript URL encoding and decoding_javascript skills

WBOY
Release: 2016-05-16 18:29:44
Original
1068 people have browsed it

In some delivery pages, GB2312 is used, while UTF8 is used in the receiving page, so the received parameters may be inconsistent with the original ones. The results are different between URLs encoded using the server-side urlEncode function and URLs encoded using the client-side JavaScript's encodeURI function.
Encoding method in javaScript:
escape() method:
Using the ISO Latin character set to encode the specified string. All spaces, punctuation marks, special characters and other non-ASCII characters will be converted into character encoding in %xx format (xx is equal to the hexadecimal number of the character's encoding in the character set table). For example, the encoding corresponding to the space character is . 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 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 " ."
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 the URI string into an escape format string using UTF-8 encoding format. Characters that will not be encoded by this method: ! @ # $& * ( ) = : / ; ? '

English explanation: 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 the URI string into an escape format string using UTF-8 encoding format. Compared with encodeURI(), this method will encode more characters, such as / and other characters. Therefore, if the string contains several parts of the URI, you cannot use this method to encode, 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 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 Chinese strings, if you do not want to convert the string encoding format into UTF-8 format (such as the original page and target When the charset of the page is consistent), you only need to use escape. If your page is GB2312 or other encoding, and the page that accepts parameters is UTF-8 encoded, you must use encodeURI or encodeURIComponent.
In addition, encodeURI/encodeURIComponent was introduced after javascript1.5, and escape was available in javascript1.0.

English note: 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 shortcoming, you should 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 should be used in most
cases when encoding
a single component of a URI. This method will encode certain chars that would normally be recognized as special chars for URIs so that many components may be included.
Note that this method
does not encode the ' character, as it is a valid character within URIs.

1. Encoding processing Function
1) encodeURI returns a result of encoding the 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 " > Output The results are as follows: encodeStr: http://www.amigoxie.com/index.jsp?name=amigoxie decodeStr: http://www.amigoxie.com/index.jsp?name=xind
2. Numerical processing Function
1) parseInt converts the base specified by a string into an integer. The syntax format is: parseInt(numString, [radix]) The first parameter is the string to be converted, which is between 2 and 36 The value between is used to specify the base used for string conversion. Examples are as follows: The output results are as follows: The default result: 32:32;032:26;0x32:50 The result converted to binary: 32:NaN;032:0;0x32:0 The result converted to octal :32:26;032:26;0x32:0 The result of conversion to hexadecimal: 32:50;032:50;0x32:50 11001010 The result after conversion: binary: 202; hexadecimal: 285216784 octal System: 2359816; Decimal: 11001010 43abc after conversion: 43; abc43 after conversion: NaN; after abc conversion: NaN
2) parseFloat method This method converts a string into the corresponding decimal. eg. The output results are as follows: 4.11 5.1 3) isNaN method This method is used to detect whether the return value of the first two methods is non-numeric. If so, return true, otherwise, return false

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template