JavaScript global function unescape () Escape () encodeURI () decodeURI () decodeuricomponent () encodeURIComponent ()

Source: Internet
Author: User


The unescape () function decodes a string encoded by escape ().

Grammar
Unescape (String) string is required. The string to decode or reverse.
return value

A copy of the string that was decoded.

Description
This function works by finding a sequence of characters in the form%xx and%uxxxx (x for hexadecimal digits), decoding with Unicode characters \U00XX and \uxxxx replacing such sequences of characters.

Hints and Notes
Note: ECMAScript v3 has removed the unescape () function from the standard and opposes its use, so it should be replaced with decodeURI () and decodeURIComponent ().

<script type= "Text/javascript" >
var test1= "Visit w3school!"
Test1=escape (test1)
document.write (test1 ")//Visit%20w3school%21
Test1=unescape (test1)
document.write (test1 ")//Visit w3school!
</script>




The Escape () function encodes the string so that it can be read on all computers.


Grammar
Escape (String) 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.
Note: ECMAScript v3 against using this method, the app uses decodeURI () and decodeuricomponent () to replace it.




encodeURI () function
The string can be encoded as a URI.

Grammar
encodeURI (uristring) 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:;/?:@&=+$,#

Tip: If the URI component contains delimiters, such as? and #, each component should be encoded using the encodeURIComponent () method.

<script type= "Text/javascript" >
document.write (encodeURI ("http://www.xxx.com"))//http://www.xxx.com
document.write (encodeURI ("Http://www.xxx.com/My first/"))//http://www.xxx.com/My%20first/
document.write (encodeURI (",/?:@&=+$#"))//,/?:@&=+$#
</script>




decodeURI () function
The URI encoded by the encodeURI () function can be decoded.

Grammar
decodeURI (uristring) uristring required. A string that contains the URI to decode or other text to decode.
Returns a copy of the value uristring, where the hexadecimal escape sequence is replaced by the character they represent.

<script type= "Text/javascript" >
var test1= "Http://www.xxx.com/My first/"
document.write (encodeURI (test1))//http://www.xxx.com/My%20first/
document.write (decodeURI (test1))//Http://www.xxx.com/My first/
</script>




decodeuricomponent () function
The URI encoded by the encodeURIComponent () function can be decoded.


Grammar
decodeURIComponent (uristring) uristring required. A string that contains the encoding URI component or other text to decode.
return value
A copy of the uristring, where the hexadecimal escape sequence is replaced by the character they represent.

<script type= "Text/javascript" >
var test1= "Http://www.xxx.com/My first/"
document.write (encodeURIComponent (test1))//http%3a%2f%2fwww.xxx.com%2fmy%20first%2f
document.write (decodeURIComponent (test1))//Http://www.xxx.com/My first/
</script>




encodeuricomponent () function
The string can be encoded as a URI component.

Grammar
encodeURIComponent (uristring) 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.

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.

<script type= "Text/javascript" >
document.write (encodeURIComponent ("http://www.xxx.com"))//http%3a%2f%2fwww.xxx.com
document.write (encodeURIComponent ("Http://www.xxx.com/My first/"))//http%3a%2f%2fwww.xxx.com%2fmy%20first%2f
document.write (encodeURIComponent (",/?:@&=+$#"))//%2c%2f%3f%3a%40%26%3d%2b%24%23
</script>

JavaScript global function unescape () Escape () encodeURI () decodeURI () decodeuricomponent () encodeURIComponent ()

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.