2 Example(s) of Javascript decodeURIComponent functions


Description :

This decodeURIComponent() function decodes a Uniform Resource Identifier (URI) means it will remove all the escape sequences in the provided URI which was encoded using encodeURIComponent. See the code snippet:


Javascript decodeURIComponent functions Example - 1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
<button onclick="decryptUriComponent()">Original uri component</button>
<p id="sample"></p>
<script>
function decryptUriComponent() {
    var uri = "http://LearnKode.com/my page.asp?name=ståle&car=saab";
    var enc = encodeURIComponent(uri);
    var dec = decodeURIComponent(enc);
    var result = "Encoded URI: " + enc + "<br>" + "Decoded URI: " + dec;
    document.getElementById("sample").innerHTML = result;
}
</script>
</body>
</html>

Output

Description :

In this example, decodeURIComponent will remove all "%20" with spaces because %20 is a replacement for spaces and "Javascript%20decodeURIComponent%20functions" text will get converted to "Javascript decodeURIComponent functions". See the code snippet:


Javascript decodeURIComponent functions Example - 2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
<body>
<button onclick="decryptUriComponent()">Original uri component</button>
<p id="sample"></p>
<script>
function decryptUriComponent() {
       var dec = decodeURIComponent("Javascript%20decodeURIComponent%20functions");
        document.getElementById("sample").innerHTML = dec;
}
</script>
</body>
</html>

Output