encodeURI (), encodeURIComponent (), decodeURI (), decodeuricomponent () URL encoding
The encodeURI () and encodeURIComponent () methods of the global object encode the URI for sending to the browser. A valid URI cannot contain certain characters, such as an empty
Pane These two URI encoding methods 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.w3cmm.com/illegal value.htm), and encodeURIComponent () Primarily used to encode a segment in a URI, such as illegal value.htm in the preceding URI. The main difference is that encodeURI () does not encode special characters of its own term URI, 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.w3cmm.com/illegal Value.htm#start";
"Http://www.w3cmm.com/illegal%20value.htm#start"
Alert (encodeURI (URI));
"Http%3a%2f%2fwww.w3cmm.com%2fillegal%20value.htm%23start"
Alert (encodeURIComponent (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 with encodeURI (), because in practice it is more common to encode query string parameters rather than base URIs.
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 space, but does not do anything with%23 because%23 represents the well font size (#), and the well font size is not replaced with encodeURI. 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.w3cmm.com%2fillegal%20value.htm%23start"
"Http%3a%2f%2fwww.w3cmm.com%2fillegal Value.htm%23start"
Alert (decodeURI (URI));
"Http://www.w3cmm.com/illegal Value.htm#start"
Alert (decodeURIComponent (URI));
Here, the variable URI contains a string encoded by encodeURIComponent (). In the result of the first call to the decodeURI () output, 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, and an escaped string is obtained.
The URI Method encodeURI (), encodeURIComponent (), decodeURI (), and decodeuricomponent () are used to replace the escape () and ECMA-262 () that have been discarded by the third version of the unescape () Method. The URI method can encode all Unicode characters, whereas the original method can encode ASCII characters correctly. Therefore, in the development practice, especially in the product-level code, be sure to use the URI method, do not use the escape () and the Unescape () method.
Encodeuri,encodeuricomponent encoding