Skip to content

Instantly share code, notes, and snippets.

@ThaddeusJiang
Created August 3, 2018 02:43
Show Gist options
  • Save ThaddeusJiang/57855ee5816c573d89b40ebcf28c7846 to your computer and use it in GitHub Desktop.
Save ThaddeusJiang/57855ee5816c573d89b40ebcf28c7846 to your computer and use it in GitHub Desktop.
escape encodeURI encodeURIComponent 区别

escape

对 String 进行编码。

ASCII字母、数字、@*/+ 不会被编码

encodeURI 和 encodeURIComponent

encodeURI

对 URL 进行编码。

不会编码的范围: ASCII字母、数字、~!@#$&*()=:/,;?+'

encodeURI("http://www.cnblogs.com/season-huang/some other thing");
// "http://www.cnblogs.com/season-huang/some%20other%20thing";

encodeURIComponent

对 URL 进行编码。

不会编码的范围: ASCII字母、数字、~!*()'

encodeURIComponent("http://www.cnblogs.com/season-huang/some other thing");
// "/" 也会被编码,整个 URL 都无法使用了
// "http%3A%2F%2Fwww.cnblogs.com%2Fseason-huang%2Fsome%20other%20thing"

所以,当你需要编码 URL 的参数的时候, encodeURIComponent 最合适。

因为参数中如果有 "/" 也会被编码,更完善。

ref: https://www.cnblogs.com/season-huang/p/3439277.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment