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"