-3

I am taking a parameter value from the URL which is as follows:

users%2F3

Where it should be users/3

How can I decode the value I am getting using Javascript?

I tried the following before:

var a = "users%2F3"; 
console.log(decodeURI(a));

But the log shows the same encoded value.

1

4 Answers 4

1

With the imaginatively named decodeURIComponent function. (See also the specification).

Sign up to request clarification or add additional context in comments.

3 Comments

I tried that before as follows: var a = "users%2F3"; console.log(decodeURI(a)); and the log shows the same value ...
@MiguelMoura Yes, a completely different function (decodeURI) doesn't do the same thing as decodeURIComponent.
Ah now I see what I was doing wrong. Thanks.
0

CODE for decode URL html code

<!DOCTYPE html>
<html>
<body>

<p>Click the button to decode a URI after encoding it.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
var uri = "users%2F3";
var uri_dec = decodeURIComponent(uri);
var res = "Decoded URI: " + uri_dec;
document.getElementById("demo").innerHTML = res;
}
 </script>

</body>
</html>

Comments

-1

If you want you can create your own function like

function urldecode(str) { return decodeURIComponent((str+'').replace(/\+/g, '%20')); };

this will also replace the '+' from the decodeURI component into spaces

Comments

-2

you can use

decodeURIComponent(a)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.