-4

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.

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481

4 Answers4

1

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

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
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>
Mahi
  • 1,707
  • 11
  • 22
-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

-2

you can use

decodeURIComponent(a)

Marc
  • 6,154
  • 1
  • 15
  • 10