Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript escape vs urlencode of non-ascii strings [duplicate]

Tags:

javascript

  • Context: HTML web page (jade/node.js/javascript)
  • image url has strings with spaces/non-ascii characters
  • I need to encode these strings either with escape or urlencode.
  • as of now I am using escape to encode spaces specifically.

But I am not able justify/understand whether I should use escape to encode strings or should I use encodeurlcomponent. I tried reading some online blogs but not understood yet.

Any pointers to which one should be used under what circumstances.

like image 421
GJain Avatar asked Mar 24 '23 12:03

GJain


1 Answers

  • escape() will not encode: @*/+

  • encodeURI() will not encode: ~!@#$&*()=:/,;?+'

  • encodeURIComponent() will not encode: ~!*()'

For more information, have a look at these questions:

Encode URL in JavaScript?

When are you supposed to use escape instead of encodeURI / encodeURIComponent?

like image 177
Danny Beckett Avatar answered Apr 07 '23 05:04

Danny Beckett