Check out the built-in function encodeURIComponent(str) and encodeURI(str), this should work:
var myOtherUrl =
"http://example.com/index.html?url=" + encodeURIComponent(myUrl);