Javascript Reference - JavaScript decodeURIComponent() Function








The decodeURIComponent() function decodes a URI component.

Browser Support

decodeURIComponent Yes Yes Yes Yes Yes

Syntax

var v = decodeURIComponent(uri)

Parameter Values

Parameter Description
uri Required. The URI to be decoded




Return Value

A String type value representing the decoded URI.

Example

decodeURIComponent() decodes all special values.


var uri = "http%3A%2F%2Fwww.java2s.com%2Fillegal%20value.htm%23start";
console.log(decodeURI(uri));
console.log(decodeURIComponent(uri));

The code above generates the following result.

Example 2

The following code shows how to Decode a URI after encoding it.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<!--from ww  w  .  ja  v  a 2 s .  c  om-->
<p id="demo"></p>

<script>
function myFunction() {
    var uri = "http://example.com/my test.asp";
    var uri_enc = encodeURIComponent(uri);
    var uri_dec = decodeURIComponent(uri_enc);
    var res = "Encoded URI: " + uri_enc + "<br>" + "Decoded URI: " + uri_dec;
    document.getElementById("demo").innerHTML = res;
}
</script>

</body>
</html>

The code above is rendered as follows: