encodeURIComponent () - это путь.
var myOtherUrl = "http://example.com/index.html?url=" + encodeURIComponent(myUrl);
НО вы должны иметь в виду, что существуют небольшие отличия от php-версии urlencode (), и, как упоминалось в @CMS, она не будет кодировать каждый голец. Ребята в http://phpjs.org/functions/urlencode/ сделали js эквивалентным phpencode ():
function urlencode(str) {
str = (str + '')
.toString();
// Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current
// PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following.
return encodeURIComponent(str)
.replace(/!/g, '%21')
.replace(/'/g, '%27')
.replace(/\(/g, '%28')
.
replace(/\)/g, '%29')
.replace(/\*/g, '%2A')
.replace(/%20/g, '+');
}