Javascript / jQuery encode a URL

March 28, 2012 — Leave a comment

Javascript has a built in function encodeURIComponent(str) and encodeURI(str) to encode strings into url.

encodeURI()

This function encodes special characters, except: , / ? : @ & = + $ #

var url = "1234 fake street";
url = encodeURI(url);
alert(url); // this prints out "1234%20fake%20street"

encodeURIComponent(str)

This function encodes special characters. In addition, it encodes the following characters: , / ? : @ & = + $ #

var url ="san jose, ca";
url = encodeURIComponent(url);
alert(url); // this prints out "san%20jose%C%20ca"

Mike

Posts Twitter Facebook

My name is Michael Le, and I am a web developer.

No Comments

Be the first to start the conversation.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.