DEV Community

Dave Mitten
Dave Mitten

Posted on

Decoding URL params with special characters

Right, so straight to it. Same as encode.

Using javascript, you want to decode an encoded email param with a special character/s that has been past in a query in a URL.

Simples...

All we have to do is utilise the function decodeURIComponent.

(This isn't a search URL params tips article, but if you don't know, this would be a way if you have access to the window and the app utilises standard URLs)

const urlParams = new URLSearchParams(window.location.search);
const email = urlParams.get('email');
Enter fullscreen mode Exit fullscreen mode

Now we have the email variable, we can start to decode. This would work with any URL params. To do other params, change the string passed into the urlParams get method.

const url = `https://url.com/?email=${encoded}`;
const decodedEmail = decodeURIComponent(email);

console.log(decodedEmail); 
// "https://url.com/?email=test%2B1%40test.com"
Enter fullscreen mode Exit fullscreen mode

and voila...problem solved.

Top comments (0)