The use of encodeURI (), encodeURIComponent (), decodeURI (), decodeURIComponent () is described in detail with examples

Source: Internet
Author: User
Tags decode all alphanumeric characters

Before we introduce the encodeURI (), encodeURIComponent (), decodeURI (), decodeURIComponent () methods, we need to understand the concept of the global object:
The global (Global) object can be said to be the most special object in ECMAScript, because no matter what angle you look at it, this object is not there. The global object in ECMAScript is defined in a sense as the ultimate "backstop object". In other words, properties and methods that do not belong to any other object are ultimately its properties and methods. In fact, there are no global variables or global functions; All properties and functions defined in global scope are properties of global objects. The functions described earlier in this book, such as isNaN (), Isfinite (), PARSELNT (), and parsefloat (), are actually all global object methods. In addition, the global object contains several other methods.
URI Encoding method
The encodeURI () and encodeURIComponent () methods of the global object can encode the URI (Uniform resourceidentifiers, Universal Resource Identifier) for sending to the browser. A valid URI cannot contain certain characters, such as spaces. This URI encoding method can encode URIs, which replace all invalid characters with special UTF-8 encoding, allowing the browser to accept and understand.
where encodeURI () is used primarily for the entire URI (for example, Http://www.jxbh.cn/illegal value.htm), and Encode-uricomponent () is used primarily for a segment of the URI ( For example, illegal value in the preceding URI. HTM) for encoding. The main difference is that encodeURI () does not encode special characters that are themselves URIs, such as colons, forward slashes, question marks, and well sizes, while encodeuricomponent () encodes any non-standard characters it discovers. Take a look at the following example:
var uri= "Http://www.jxbh.cn/illegal Value.htm#start";
"http://www.jxbh.cn/illegal%20value. htm#s Tart"
Alert (encodeURI (URI)):
"http% 3a%2f%2fwww.jxbh.cn%2 fillegal%2 0value. htm%23 Start "
Alert (Encodauricomponent (URI));
The result of using encodeURI () encoding is that characters other than spaces are left intact, and only spaces are replaced with%20. The encodeURIComponent () method replaces all non-alphanumeric characters with the corresponding encoding. This is also the reason why you can use encodeURI () for the entire URI and only use encodeURIComponent () for strings appended to the existing URI. In general, we use the encodeURIComponent () method more often than encodeURI (), because in practice it is more common to encode query string parameters instead of the underlying URL.
The two methods corresponding to the encodeURI () and encodeURIComponent () methods are decodeURI () and decodeURIComponent () respectively. where decodeURI () can only decode characters that are replaced with encodeURI (). For example, it can replace%20 with a single space, but does not do anything with%23 because%23 represents the well font size (#), and the well font size is not replaced with a encodeur (). Similarly, decodeuricomponent () can decode all characters encoded using encodeuricomponent (), that is, it can decode the encoding of any special character. Take a look at the following example:
var uri= "http%3a%2f%2fwww.jxbh.cn%2fillegal%2 0value.htm%23 start";
http% 3a%2f%2fwww. jxbh.cn%2 fillegal value. htm%23 Start
Alert (decodeURI (URI));
http://www.jxbh.cn/illegal value. htm# Start
Alert (decodeURIComponent (URI));
Here, the variable URI contains a string encoded by encodeURIComponent (). The first call to decodeURI ()
The result of the output is that only%20 is replaced with a space. In the result of the second call to the decodeURIComponent () output, the encoding of all special characters is replaced with the original character, resulting in an escaped string (but the word: a valid URI).
Nanchang website Construction companyThe network engineer hints that the URI method encodeURI (), encodeURIComponent (), decodeURI (), and decodeuricomponent () are used to replace the escape () that has been discarded by ECMA-262 version 3rd and the Unescape () method, the URI method is capable of encoding all Unicode characters, and the original method can only encode ASCII characters correctly. Because in development practices, especially in product-level code, you must use the URI method. Do not use Escape () and unescape ( Method.

The use of encodeURI (), encodeURIComponent (), decodeURI (), decodeURIComponent () is described in detail with examples

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.