encodeURI
和 encodeURIComponent
的区别
encodeURI
加密一些URL中不允许的字符, 如空格等
var url = 'http://xx xxx/';
encodeURI(url);
// 输出: "http://xx%20xxx/"
encodeURIComponent
加密query string中的一些特殊字符
var url = 'http://xx xxx/';
encodeURIComponent(url);
// 输出: "http%3A%2F%2Fxx%20xxx%2F"
总结
当使用 http://www.judith.com?url=http://www.baidu.com 这种 query string 中带有 http:// 特殊字符时, 可以使用encodeURIComponent
来进行加密