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