Javascript Reference - JavaScript decodeURI() Function








The decodeURI() function is used to decode a URI.

Browser Support

decodeURI Yes Yes Yes Yes Yes

Syntax

var v = decodeURI(uri)

Parameter Values

Parameter Description
uri Required. The URI to be decoded




Return Value

A String type value representing the decoded URI.

Example

The decodeURI() method decodes only characters that would have been replaced by using encodeURI().


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>
<p id="demo"></p>
<!--  w ww. j  av  a 2 s.  c o  m-->
<script>
function myFunction() {
    var uri = "my test.asp?name=st?le&car=saab";
    var enc = encodeURI(uri);
    var dec = decodeURI(enc);
    var res = "Encoded URI: " + enc + "<br/>" + "Decoded URI: " + dec;
    document.getElementById("demo").innerHTML = res;
}
</script>

</body>
</html>

The code above is rendered as follows: