encodeURI

Encodes a Uniform Resource Identifier (URI) by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two "surrogate" characters).
codice:
var str="http://www.url.con/alcuni spazi/inseriti qui e  li/";
console.log(encodeURI(str));

/* http://www.url.con/alcuni%20spazi/in...i%20e%20%20li/ */