Skip to main content

jQuery Encode/Decode URL String

By Sam Deering



Free JavaScript Book!

Write powerful, clean and maintainable JavaScript.

RRP $11.95

Simple jQuery code snippet to encode/decode (convert) a href params in a url string (http address) so that they can be properly viewed on a web page. For example, %20 is the html equivalent of a space and %40 is an ampersand (@).

Encode URL String

var url = $(location).attr('href'); //get current url
var url = 'folder/index.html?param=#23dd&noob=yes'; //or specify one

var encodedUrl = encodeURIComponent(url);
//outputs folder%2Findex.html%3Fparam%3D%2323dd%26noob%3Dyes

Decode URL String

var url = $(location).attr('href'); //get current url
var url = 'folder%2Findex.html%3Fparam%3D%2323dd%26noob%3Dyes'; //or specify one

var decodedUrl = decodeURIComponent(url);
//outputs  folder/index.html?param=#23dd&noob=yes

HTML URL Endoding References

space 	%20
! 	%21
" 	%22
# 	%23
$ 	%24
% 	%25
& 	%26
' 	%27
( 	%28
) 	%29
* 	%2A
+ 	%2B
, 	%2C
- 	%2D
. 	%2E
/ 	%2F

See all HTML URL Encoding References

Related Articles

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

New books out now!

Learn the basics of programming with the web's most popular language - JavaScript

A practical guide to leading radical innovation and growth.

Integromat Tower Ad