The decodeURIComponent()
function decodes a URI component.
decodeURIComponent |
Yes | Yes | Yes | Yes | Yes |
var v = decodeURIComponent(uri)
Parameter | Description |
---|---|
uri | Required. The URI to be decoded |
A String type value representing the decoded URI.
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.
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: