Home >Web Front-end >JS Tutorial >Detailed introduction to several methods of javascript coding_Basic knowledge
escape() function encodes a string so that it can be read on all computers.
参数 | 描述 |
---|---|
string | 必需。要被转义或编码的字符串。 |
A copy of the encoded string. Some of these characters are replaced with hexadecimal escape sequences.
DescriptionThis method does not encode ASCII letters and numbers, nor does it encode the following ASCII punctuation characters: * @ - _ . / . All other characters will be replaced by escape sequences.
Tips and Notes
Tip: You can use unescape() to decode escape() encoded strings.
Note: ECMAScript v3 deprecates this method, applications should use decodeURI() and decodeURIComponent() instead.
Example:encodeURI definition and usage
The encodeURI() function encodes a string as a URI.
Syntax encodeURI(URIstring)参数 | 描述 |
---|---|
URIstring | 必需。一个字符串,含有 URI 或其他要编码的文本。 |
A copy of the URIstring, with some characters replaced by hexadecimal escape sequences.
DescriptionThis method does not encode ASCII letters and numbers, nor does it encode these ASCII punctuation characters: - _ . ! ~ * ' ( ) .
The purpose of this method is to completely encode the URI, so the encodeURI() function will not escape the following ASCII punctuation marks that have special meaning in the URI: ;/?:@&= $, #
Tips and Notes
Can be decoded with decodeURI()
Tip: If the URI component contains delimiters, such as ? and #, you should use the encodeURIComponent() method to encode each component separately.
ExampleDefinition and usage
The encodeURIComponent() function encodes a string as a URI component.
GrammarencodeURIComponent(URIstring)
参数 | 描述 |
---|---|
URIstring | 必需。一个字符串,含有 URI 组件或其他要编码的文本。 |
Description
A copy of the URIstring, with some characters replaced by hexadecimal escape sequences. Description
This method does not encode ASCII letters and numbers, nor does it encode these ASCII punctuation characters: - _ . ! ~ * ' ( ) .
Other characters (such as :;/?:@&= $,# which are punctuation marks used to separate URI components) are replaced by one or more hexadecimal escape sequences.
Tips and Notes
Tip
: Please note that the encodeURIComponent() function differs from the encodeURI() function in that the former assumes that its arguments are part of a URI (such as protocol, hostname, path, or query string). The encodeURIComponent() function therefore escapes the punctuation characters used to separate parts of the URI. Example