1、最经常使用的encodeURI和encodeURIComponent编码
encodeURI("http://www.cnblogs.com/season-huang/some other thing");
"http://www.cnblogs.com/season-huang/some%20other%20thing";
其中,空格被编码成了%20。可是若是你用了encodeURIComponent,那么结果变为url
"http%3A%2F%2Fwww.cnblogs.com%2Fseason-huang%2Fsome%20other%20thing"
看到了区别吗,连 "/" 都被编码了,整个URL已经无法用了。spa
三、当你须要编码URL中的参数的时候,那么encodeURIComponent是最好方法。code
var param = "http://www.cnblogs.com/season-huang/"; //param为参数
param = encodeURIComponent(param);
var url = "http://www.cnblogs.com?next=" + param;
console.log(url) //"http://www.cnblogs.com?next=http%3A%2F%2Fwww.cnblogs.com%2Fseason-huang%2F"