Home  >  Article  >  Web Front-end  >  Introduction to js encoding and decoding functions and their usage examples_Basic knowledge

Introduction to js encoding and decoding functions and their usage examples_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:23:211252browse

JS encoding text involves 3 functions: escape, encodeURI, encodeURIComponent, corresponding to 3 decoding functions: unescape, decodeURI, decodeURIComponent

1. When passing parameters, you need to use encodeURIComponent, this combination The URL will not be truncated by special characters such as #.
For example:


2. You can use encodeURI as a whole when performing url jump

For example: Location.href=encodeURI(http://cang .baidu.com/do/s?word=中国&ct=21);
Characters encoded using this method can be decoded using the urldecode() function in PHP
3. js use When using data, you can use escape

escape to output %u**** format when encoding unicode values ​​other than 0-255. In other cases, the encoding results of escape, encodeURI, and encodeURIComponent are the same.
There are 69 unencoded characters in escape: *, , -, ., /, @, _, 0-9, a-z, A-Z
There are 82 unencoded characters in encodeURI: !, #, $, &, ', (,), *, ,,,-,.,/,:,;,=,?,@,_,~,0-9,a-z,A-Z
encodeURIComponent has 71 unencoded characters: ! , ',(,),*,-,.,_,~,0-9,a-z,A-Z

Attached is the introduction of these three functions:


escape method
Encodes String objects so that they are readable on all computers,
escape(charString)
Required charstring parameter is any String object or text to be encoded.
Description
The escape method returns a string value (Unicode format) containing the content of charstring. All spaces, punctuation, accents, and other non-ASCII characters are replaced with the %xx encoding,
where xx is equal to the hexadecimal number representing the character. For example, spaces are returned as " ".
Character values ​​greater than 255 are stored in %uxxxx format.
Note that the escape method cannot be used to encode Uniform Resource Identifiers (URIs). It should be encoded using the encodeURI and encodeURIComponent methods.

encodeURI method
Encodes a text string into a valid Uniform Resource Identifier (URI).
encodeURI(URIString)
The required URIString parameter represents an encoded URI.
Description
The encodeURI method returns an encoded URI. If you pass the encoding result to decodeURI, the original string will be returned. The encodeURI method does not encode the following characters: ":",
"/", ";" and "?". Please use the encodeURIComponent method to encode these characters.

encodeURIComponent method
Encodes a text string into a valid component of a Uniform Resource Identifier (URI).
encodeURIComponent(encodedURIString)
The required encodedURIString parameter represents an encoded URI component.
Description

The encodeURIComponent method returns an encoded URI. If you pass the encoding result to decodeURIComponent, the original string will be returned. Because the encodeURIComponent
method encodes all characters, please note that if the string represents a path, such as /folder1/folder2/default.html, the slashes in it will also be encoded. As a result, the encoding result will be invalid when sent as a request to the web server. If the string contains more than one URI component, use the encodeURI method to encode it.

Statement:
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