1:escape()函数不能直接用于url编码,它的真正作用是返回一个字符的Unicode编码值。
如:escape('hello world');//hello%20world
2:encodeURI()除了常见的符号以外,对网址中有特殊含义的符号:/ ? : @ & = + $ #等不进行编码。编码后,它输出符号的utf-8形式,并且在每个字节前加上%。
如:console.log(encodeURI('http://www.baidu.com?name=张三&age=18#'));//http://www.baidu.com?name=%E5%BC%A0%E4%B8%89&age=18#
3:encodeURIComponent(),像”; / ? : @ & = + $ , #”,这些在encodeURI()中不被编码的符号,在encodeURIComponent()中统统会被编码。
4:encodeURI、encodeURIComponent都不会对ASCII 字母和数字进行编码,也不会对这些 ASCII 标点符号进行编码: - _ . ! ~ * ’ ( ) 。
总结:因此,我们对url进行编码时一般用:encodeURIComponent()函数