Difference between escape(), encodeURI(), encodeURIComponent()

前端 未结 5 1265
走了就别回头了
走了就别回头了 2021-01-30 00:32

In JavaScript, what is the difference between these?

  1. escape() / unescape()
  2. encodeuri() / decodeuri()
5条回答
  •  北恋
    北恋 (楼主)
    2021-01-30 00:45

    First of all - Escape is deprecated and shouldn't be used.

    encodeURI()

    You should use this when you want to encode a URL, it encodes symbols that is not allowed in a URL.

    encodeURIComponent()

    Should be used when you want to encode parameters of your URL, You can also use this to encode a whole URL. But you would have to decode it in order to use it again.

    --

    I'd say this a duplicate. Here's a good answer on SO - Credits goes to Arne Evertsson: When are you supposed to use escape instead of encodeURI / encodeURIComponent?

    There's a lot of details on why/why not on that topic.

提交回复
热议问题