# Encode URL in JavaScript?

How to encode a URL using JavaScript, which 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;

There's a built-in function available encodeURIComponent(str) and encodeURI(str).
The following will work for you

There's a built-in function available encodeURIComponent(str) and encodeURI(str).
The following will work for you

var myOtherUrl =

"http://example.com/index.html?url=" + encodeURIComponent(myUrl);