區(qū)別:
escape vs (encodeURI && encodeURIComponent)
- 在處理0xff之外的字符方式有區(qū)別进统,escape 是直接使用字符的 unicode 在前面加上一個 「%u」encodeURI助币、encodeURIComponent則是先進行 UTF-8,再在 UTF-8 的每個字節(jié)碼前加上一個 「%」螟碎;例如"浩鴿鴿"轉(zhuǎn)譯后眉菱,
escape: %u6D69%u9E3D%u9E3D,
encodeURI || encodeURIComponent:"%E6%B5%A9%E9%B8%BD%E9%B8%BD"
- encodeURI 是W3C 的標準,而 Escape 是非標準掉分。
encodeURI vs encodeURIComponent
- encodeURI 不會對url中的功能字符進行轉(zhuǎn)譯俭缓,例如&, ?, /, =等等這些。
場景:
- escape指對字符串進行編碼酥郭,如果是對URI進行操作尔崔,那么不就不要使用escpae。
- encodeURI 通常用來對整個URI 進行編碼褥民,encodeURIComponent 用來對傳遞數(shù)據(jù)的Key,Value進行編碼。