How to encode a URL in JavaScript?

0 votes
110 views
asked Nov 28, 2013 in java script by anonymous

How do you safely encode a URL using JavaScript such that it can be put into a GET string?

var myUrl = "http://example.com/index.html?param=1&anotherParam=2";
var myOtherUrl = "http://example.com/index.html?url=" + myUrl;

I assume that you need to encode the myUrl variable on that second line?

1 Answer

0 votes
answered Nov 29, 2013 by Ryan

Reference:  

http://www.w3schools.com/tags/ref_urlencode.asp

encodeURIComponent(value).replace(/%20/g,'+')

...