Use and difference between JavaScript decodeURI () and decodeURIComponent ()

Source: Internet
Author: User

decodeURI () Definition and usage: the decodeURI () function decodes a URI encoded by the encodeURI () function.
Syntax: decodeURI (uristring)
Parameter description: Uristring required, a string containing the URI group to decode or other text to decode.
Return value: A copy of uristring, where the hexadecimal escape sequence is replaced by the character they represent.

decodeURIComponent () Definition and usage: the decodeURIComponent () function decodes a URI encoded by the encodeURIComponent () function.
Syntax: decodeURIComponent (uristring)
Parameter description: Uristring required, a string that contains the decoded URI component or other text to decode.
Return value: A copy of uristring, where the hexadecimal escape sequence is replaced by the character they represent.

The above is a description of the usage, but there are some problems in the actual use process:

#特殊符号进行进行编码传递参数的时候有一些不一样:

Test code as follows:

<script>

Function Demo () {

var text=escape ("Http://www.w3school.com.cn/My first/#qpp");
alert (text);

}
function Demo1 ()
{
var test1= "Http://www.w3school.com.cn/My first/#qpp"
Alert (encodeURIComponent (test1));

}
function Demo3 ()
{
var test1= "Http://www.w3school.com.cn/My first/#qpp"
Alert (decodeURI (test1));

}
function MyApp (text)
{
var text=unescape (text);
alert (text);
}
</script>

<body>
<input type= "button" onclick= "Demo ()" Value= "Escape"/><br>
<input type= "button" onclick= "Demo3 ()" value= "decodeURI"/><br>
<input type= "button" onclick= "Demo1 ()" value= "encodeURIComponent"/><br>
<input type= "button" onclick= "MyApp (' visit%20w3school%21 ')" value= "Unescape"/><br>
</body>

The test is as follows:

For the above test, it can be helpful to have the # wait for a special character in the query string.

Use and difference between JavaScript decodeURI () and decodeURIComponent ()

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.