nickf nickf - 26 days ago 4x
Javascript Question

Encode URL in JavaScript?

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

var myUrl = "";
var myOtherUrl = "" + myUrl;

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


Check out the built-in function encodeURIComponent(str) and encodeURI(str).
In your case, this should work:

var myOtherUrl = 
       "" + encodeURIComponent(myUrl);