var url = "http://www.wrox.com/illegal value.html#start";
encodeURIComponent(url)  //"http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.html%23start"

encodeURI(url)   //"http://www.wrox.com/illegal%20value.html#start"

 

记住2种特殊字符,一种,浏览器无法识别的特殊字符,如空格、中文。第二种,属于url的特殊字符,如/、//、# 、&等。
encodeURI方法只把第一种特殊字符转义,而encodeURIComponent方法会把两种特殊字符都转义。如果我们需要把url作为参数传给服务器是要用encodeURIComponent方法的。
 

encodeURIComponent() 函数可把字符串作为 URI 组件进行编码。

decodeURIComponent() 解码

encodeURI 编码

decodeURI 解码

相关文章:

  • 2022-12-23
  • 2021-11-19
  • 2021-11-28
  • 2022-01-29
  • 2022-12-23
猜你喜欢
  • 2022-03-03
  • 2021-08-13
  • 2022-12-23
  • 2021-11-02
  • 2021-12-21
  • 2022-12-23
  • 2021-10-02
相关资源
相似解决方案