Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

In JavaScript, what is the difference between these?

  1. escape() / unescape()
  2. encodeuri() / decodeuri()
  3. encodeURIComponent() / decodeURIComponent()
like image 750
jatin patil Avatar asked Jan 14 '13 11:01

jatin patil


People also ask

What is the difference between encodeURI and encodeURIComponent?

The difference between encodeURI and encodeURIComponent is encodeURIComponent encodes the entire string, where encodeURI ignores protocol prefix ('http://') and domain name. encodeURIComponent is designed to encode everything, where encodeURI ignores a URL's domain related roots.

Should I use encodeURI or encodeURIComponent?

encodeURIComponent should be used to encode a URI Component - a string that is supposed to be part of a URL. encodeURI should be used to encode a URI or an existing URL.

What is encodeURI?

The encodeURI() function encodes a URI by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two "surrogate" characters).

What is encodeURI and decodeURI in JavaScript?

encodeURI() and decodeURI() functions in JavaScript. The encodeURI() function encodes the complete URI including special characters except except (, / ? : @ & = + $ #) characters. The decodeURI() function decodes the URI generated by the encodeURI() function.


2 Answers

For the visually minded, here's a table showing the effects of encodeURI(), encodeURIComponent() and escape() on the commonly-used symbolic ASCII characters:

Char  encUrI  encURIComp  escape *     *       *           * .     .       .           . _     _       _           _ -     -       -           - ~     ~       ~           %7E '     '       '           %27 !     !       !           %21 (     (       (           %28 )     )       )           %29 /     /       %2F         / +     +       %2B         + @     @       %40         @ ?     ?       %3F         %3F =     =       %3D         %3D :     :       %3A         %3A #     #       %23         %23 ;     ;       %3B         %3B ,     ,       %2C         %2C $     $       %24         %24 &     &       %26         %26       %20     %20         %20 %     %25     %25         %25 ^     %5E     %5E         %5E [     %5B     %5B         %5B ]     %5D     %5D         %5D {     %7B     %7B         %7B }     %7D     %7D         %7D <     %3C     %3C         %3C >     %3E     %3E         %3E "     %22     %22         %22 \     %5C     %5C         %5C |     %7C     %7C         %7C `     %60     %60         %60 

Another vital difference is that unescape() does not handle multi-byte UTF-8 sequences whereas decodeURI[Component]() does:

decodeURIComponent("%C3%A9") == "é" unescape("%C3%A9") == "é" 
like image 96
chris Avatar answered Sep 28 '22 08:09

chris


  • escape — broken, deprecated, do not use
  • encodeURI — encodes characters that are not allowed (raw) in URLs (use it to fix up broken URIs if you can't fix them beforehand)
  • encodeURIComponent — as encodeURI plus characters with special meaning in URIs (use it to encode data for inserting into a URI)
like image 38
Quentin Avatar answered Sep 28 '22 08:09

Quentin