Home >Web Front-end >JS Tutorial >Comparison of javascript encodeURI and encodeURIComponent_javascript skills

Comparison of javascript encodeURI and encodeURIComponent_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:30:151424browse
Background
encodeURI and encodeURIComponent are both functions defined in the ECMA-262 standard. All languages ​​that are compatible with this standard (such as JavaScript, ActionScript) will implement these two functions. They are both global functions used to encode URI (RFC-2396) strings, but their processing methods and usage scenarios are different. In order to explain their differences, we first need to understand the classification of characters in URIs in RFC-2396
reserved characters: these characters are reserved key characters in URIs, and they are used to separate the various parts of the URI. . These characters are: ";" | "/" | "?" | ":" | "@" | "&" | "=" | " " | "$" | ","
Mark characters ): This type of character is specially defined in RFC-2396, but there is no special explanation of its use. It may be related to other RFC standards. These characters are: "-" | "_" | "." | "!" | "~" | "*" | "'" | "(" | ")"
Basic characters (alphanum characters): this Class characters are the main part of URI, which include all uppercase letters, lowercase letters and numbers
After introducing the above three types of strings, it is very easy for us to explain the difference between encodeURI and encodeURIComponent functions:
encodeURI: This function performs escaping on all non-(basic characters, Mark characters and reserved characters) in the passed string. All characters that need to be escaped are converted into one, two or three-byte hexadecimal escape characters (%xx) according to UTF-8 encoding. For example, the character space " " is converted to " ". Under this encoding mode, the ASCII characters that need to be encoded are replaced with one byte escape characters, the characters between u0080 and u007ff are replaced with two byte escape characters, and the other 16 Unicode characters are replaced with three bytes.
encodeURIComponent: There is only one difference between the processing method of this function and encodeURI, that is, escape encoding is also performed for reserved characters. For example, the character ":" is escaped by the character ":" instead of
. The reason why there are two different functions above is because we have two different encoding processing requirements for URI when writing JS code. encodeURI can be used to encode the complete URI string. The encodeURIComponent can encode a part of the URI so that this part can contain some URI reserved characters. This is very useful in our daily programming. For example, the following URI string:
http://www.mysite.com/send-to-friend.aspx?url=http://www.mysite.com/product.html
In this URI character String in. The send-to-friend.aspx page will create email content in HTML format, which will contain a link. The address of this link is the url value in the URI string above. Obviously the above url value is a part of the URI, which contains URI reserved key characters. We must call encodeURIComponent to encode it before use, otherwise the above URI string will be considered by the browser as an invalid URI. The correct URI should be as follows:
http://www.mysite.com/send-to-friend.aspx?url=http://www.mysite.com/product.html
Example
encodeURI
Copy code The code is as follows:

var uri="my test .asp?name=ståle&car=saab";
document.write(encodeURI(uri));

The above output is as follows:
my test.asp?name=ståle&car=saab

encodeURIComponent
Copy code The code is as follows:

var uri="http://jb51.net/my test.asp?name=ståle&car=saab";
document.write(encodeURIComponent(uri));

The above output As follows:
http://jb51.net/my test.asp?name=ståle&car=saab
Others
In the ECMA-262 standard, the decode global corresponding to these two encode functions is also defined Functions, they are decodeURI and decodeURIComponent. We can use them to decode the encoded string
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