What's the difference between `encodeURIComponent()` and `encodeURI()`, and how does it relate to this error?

Question

Grade: Education Subject: Support
What's the difference between `encodeURIComponent()` and `encodeURI()`, and how does it relate to this error?
Asked by:
109 Viewed 109 Answers

Answer (109)

Best Answer
(355)
`encodeURIComponent()` encodes *all* characters that have special meaning in a URI component (like `/`, `?`, `#`, `&`, `=`, etc.). `encodeURI()` only encodes characters that are reserved for delimiting URI components. Using `encodeURI()` when you need to encode the entire component can lead to malformed URIs and, subsequently, this error when decoding.