decodeURIComponent vs unescape, what is wrong with unescape?

后端 未结 4 1154
醉话见心
醉话见心 2020-11-29 23:32

In answering another question I became aware that my Javascript/DOM knowledge had become a bit out of date in that I am still using escape/unescape

4条回答
  •  时光说笑
    2020-11-30 00:33

    What I want to know is what is wrong with escape/unescape ?

    They're not “wrong” as such, they're just their own special string format which looks a bit like URI-parameter-encoding but actually isn't. In particular:

    • ‘+’ means plus, not space
    • there is a special “%uNNNN” format for encoding Unicode UTF-16 code points, instead of encoding UTF-8 bytes

    So if you use escape() to create URI parameter values you will get the wrong results for strings containing a plus, or any non-ASCII characters.

    escape() could be used as an internal JavaScript-only encoding scheme, for example to escape cookie values. However now that all browsers support encodeURIComponent (which wasn't originally the case), there's no reason to use escape in preference to that.

    There is only one modern use for escape/unescape that I know of, and that's as a quick way to implement a UTF-8 encoder/decoder, by leveraging the UTF-8 processing in URIComponent handling:

    utf8bytes= unescape(encodeURIComponent(unicodecharacters));
    unicodecharacters= decodeURIComponent(escape(utf8bytes));
    

提交回复
热议问题