Home >Web Front-end >JS Tutorial >Detailed introduction to several methods of javascript coding_Basic knowledge

Detailed introduction to several methods of javascript coding_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:44:23975browse
escape definition and usage The

escape() function encodes a string so that it can be read on all computers.

参数 描述
string 必需。要被转义或编码的字符串。

Return value

A copy of the encoded string. Some of these characters are replaced with hexadecimal escape sequences.

Description

This 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 或其他要编码的文本。

Return value

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: - _ . ! ~ * ' ( ) .

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.

Example

Definition and usage

The encodeURIComponent() function encodes a string as a URI component.

Grammar
encodeURIComponent(URIstring)
参数 描述
URIstring 必需。一个字符串,含有 URI 组件或其他要编码的文本。
Parameters

Description

URIstring Required. A string containing URI components or other text to be encoded. Return value

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

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