There are three functions in JavaScript that can encode a string, namely: escape (), encodeuri (), encodeuricomponent ()

Source: Internet
Author: User

There are three functions in JavaScript that can encode strings, namely: escape,encodeuri,encodeuricomponent, corresponding 3 decoding functions: unescape,decodeuri, Decodeuricomponent.

Here's a brief look at the difference between them

1 Escape () function

Definition and usage
The escape () function encodes the string so that it can be read on all Computers.

Grammar
Escape (string)

Parameter description
String is Required. The string to be escaped or encoded.

return value
A copy of the encoded STRING. Some of these characters are replaced with 16-binary escape sequences.

Description
The method does not encode ASCII letters and numbers, nor does it encode the following ASCII punctuation marks:-_. ! ~ * ' (). All other characters will be replaced by escape sequences.


2 encodeURI () function
Definition and usage
The encodeURI () function encodes a string as a URI.

Grammar
encodeURI (uristring)

Parameter description
Uristring Required. A string that contains the URI or other text to Encode.

return value
A copy of the uristring, where some of the characters will be replaced by a hexadecimal escape sequence.

Description
The method does not encode ASCII letters and numbers, nor does it encode these ASCII punctuation marks:-_. ! ~ * ' ().

The purpose of this method is to fully encode the uri, so the encodeURI () function is not escaped for the following ASCII punctuation mark with a special meaning in the uri:;/?:@&=+$,#


3 encodeURIComponent () function

Definition and usage
The encodeURIComponent () function encodes a string as a URI Component.

Grammar
encodeURIComponent (uristring)

Parameter description
Uristring Required. A string that contains the URI component or other text to Encode.

return value
A copy of the uristring, where some of the characters will be replaced by a hexadecimal escape sequence.

Description
The method does not encode ASCII letters and numbers, nor does it encode these ASCII punctuation marks:-_. ! ~ * ' ().

Other characters (such as:;/?:@&=+$,# These punctuation marks used to separate the URI Component) are replaced by one or more hexadecimal escape sequences.

Hints and Notes
Tip: Note the difference between the encodeURIComponent () function and the encodeURI () function, which assumes that its arguments are part of a URI (such as a protocol, hostname, path, or query string). therefore, the encodeuricomponent () function escapes punctuation marks that are used to separate the various portions of the URI.

4 summary:

Through the analysis of three functions, we can know: escape () in addition to ASCII letters, numbers and specific symbols, the passed-in strings are all escaped encoding, so if you want to encode the url, it is best not to use this method. encodeURI () is used to encode the entire URI because the legitimate characters in the URI are not converted by encoding. The encodeURIComponent method encodes a single uricomponent (the request Parameter) should be the most common, it can be described in the parameters of chinese, special characters escaped, without affecting the entire url.

5 example:

1 Escape ()

<script type= "text/javascript" >

document.write (escape ("http://www.w3school.com.cn/") + "<br/>")

document.write (escape ("?! = () #%& "))

</script> output:

http%3a//www.w3school.com.cn

%3f%21%3d%28%29%23%25%26

2 encodeURI ()

<script type= "text/javascript" >

document.write (encodeuri ("http://www.w3school.com.cn/") + "<br/>")

document.write (encodeuri ("http://www.w3school.com.cn/My First/"))

document.write (encodeuri (",/?:@&=+$#"))

</script> output:

http://www.w3school.com.cn/

http://www.w3school.com.cn/My%20first/

,/?:@&=+$#

The entire URL is encoded, and the specific identifier for the URL is not transcoded.

3 encodeURIComponent ()

Example 1:

<script type= "text/javascript" >

document.write (encodeuricomponent ("http://www.w3school.com.cn/"))

document.write ("<br/>")

document.write (encodeuricomponent ("http://www.w3school.com.cn/p 1/"))

document.write ("<br/>")

document.write (encodeuricomponent (",/?:@&=+$#"))

</script output:

http%3a%2f%2fwww.w3school.com.cn
http%3a%2f%2fwww.w3school.com.cn%2fp%201%2f
%2c%2f%3f%3a%40%26%3d%2b%24%23
Example 2:<script language= "javascript" >document.write ('

<a href= "http://passport.baidu.com/?logout&aid=7&u= ' +encodeuricomponent (" http://cang.baidu.com/ Bruce42 ") + ' > Exit </a> ');</script>

The parameters in the URL are encoded because the parameter is also a url, and if not encoded, it affects the entire URL of the Jump.

There are three functions in JavaScript that can encode a string, namely: escape (), encodeuri (), encodeuricomponent ()

Related Article

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.