Would encodeURI() work?