Javascript – Encode and decode URL like %20

When you want to create an url which contains special charaters like the whitespace, you need to convert it to %20 ascii value. In Javascript, you could simply make use of the built-in encodeURI() and decodeURI() as follow.

console.log(encodeURI("http://localhost:8000/?greeting=" + "Welcome to Eureka!"));
// Output
http://localhost:8000/?greeting=Welcome%20to%20Eureka!


console.log(decodeURI("http://localhost:8000/?greeting=Welcome%20to%20Eureka!"));
// Output
// http://localhost:8000/?greeting=Welcome to Eureka!

 

You can do it in PHP as well.
PHP – URL Encode and Decode

Done =)

Reference:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s