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

May 16, 2016 pm 06:29 PM
url encoding decoding

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

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Base64 encoding and decoding using Python Base64 encoding and decoding using Python Sep 02, 2023 pm 01:49 PM

Base64 encoding and decoding using Python

How to solve URL decoding exceptions in Java development How to solve URL decoding exceptions in Java development Jun 29, 2023 pm 02:07 PM

How to solve URL decoding exceptions in Java development

How to implement encoding and decoding of Chinese characters in C language programming? How to implement encoding and decoding of Chinese characters in C language programming? Feb 19, 2024 pm 02:15 PM

How to implement encoding and decoding of Chinese characters in C language programming?

How to use the urllib.parse.quote() function to encode URLs in Python 3.x How to use the urllib.parse.quote() function to encode URLs in Python 3.x Jul 31, 2023 pm 10:46 PM

How to use the urllib.parse.quote() function to encode URLs in Python 3.x

How to use the urllib.parse.urlencode() function to encode parameters in Python 3.x How to use the urllib.parse.urlencode() function to encode parameters in Python 3.x Aug 03, 2023 pm 03:18 PM

How to use the urllib.parse.urlencode() function to encode parameters in Python 3.x

How to reset your PIN How to reset your PIN Feb 18, 2024 pm 09:02 PM

How to reset your PIN

Try the method to solve the problem of Chinese garbled characters in Eclipse Try the method to solve the problem of Chinese garbled characters in Eclipse Jan 03, 2024 pm 05:28 PM

Try the method to solve the problem of Chinese garbled characters in Eclipse

What are the techniques for byte encoding and decoding in Python? What are the techniques for byte encoding and decoding in Python? Oct 18, 2023 am 09:27 AM

What are the techniques for byte encoding and decoding in Python?

See all articles