一、escape
escape是对字符串进行编码,使得可以在所有的电脑上可读,但是encodeURI和encodeURIComponent是对URL进行编码的。escape和后两者几乎没关系。
escape进行编码后,展示的结果类型大致为:%xxxx 或者%uxxxx。
escape是不对ascll字母,数字,@,*,/,+进行编码,其余的都会进行编码。
console.log(escape("aaa12@*/+")) //aaa12@*/+
console.log(escape("我是哈哈哈%")) //%u6211%u662F%u54C8%u54C8%u54C8%25
二、encodeURI和encodeURIComponent
encodeURI和encodeURIComponent两者都是对URL进行编码的,但是两者的区别是编码的范围不同。
encodeURI不能对ascll字符,数字,~,!,@,#,$,&,*,(),=,:,/,,,;,?,+,'进行编码。
encodeURIComponent不能对ascll字母,数字,~,!,*,(),'进行编码。
所以说encodeURIComponent比encodeURI的编码范围更大。
比如对于https://www.baidu.com这样一个url地址,encodeURI和encodeURIComponent分别对其进行编码对比如下。
console.log(encodeURI("https://www.baidu.com")) //https://www.baidu.com
console.log(encodeURIComponent("https://www.baidu.com")) //https%3A%2F%2Fwww.baidu.com
三、场合使用
1、如果只是编码字符串,没有url的事,此时使用escape。
2、如果你需要编码整个url,此时应该使用encodeURI
console.log(encodeURI("https://www.baidu.com")) //https://www.baidu.com
console.log(encodeURIComponent("https://www.baidu.com")) //https%3A%2F%2Fwww.baidu.com
如上述代码所示,如果此时我们使用encodeURIComponent,则连 / 都会进行编码,导致整个URL没有办法使用。
3、如果你需要编码参数时,可以使用encodeURIComponent。

如上图所示,如果我们使用encodeURI一定会出问题,因为后面还存在/,并且encodeURI无法进行编码,此时encodeURIComponent可以对/进行编码。
本片博客参考:escape, encodeURI,encodeURIComponent









