nickf nickf - 1 year ago 160
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?

Answer Source

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

var myOtherUrl = 
       "" + encodeURIComponent(myUrl);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download