1

Such as the title,I have try to coding a string with encodeURI method,and decoding the result with decodeURIComponent method.and then I found the decoding string same as the original string.So I want to know if all strings coding with encodeURI can be decoding using decodeURIComponent.

encodeURI("http://www.example.com?type=qqq&string=qqq+<>`www");
//"http://www.example.com?type=qqq&string=qqq+%3C%3E%60www"

decodeURIComponent("http://www.example.com?type=qqq&string=qqq+%3C%3E%60www");
//"http://www.example.com?type=qqq&string=qqq+<>`www"
dawn
  • 855
  • 1
  • 7
  • 16
  • What do you mean by encrypt/decrypt? This isn't encryption, and why use decodeURIComponent versus just decodeURI? – Devon Bessemer Sep 17 '18 at 03:24
  • Note you are using 2 different encode/decode methods, encodeURI should be decoded with decodeURI not decodeURIComponent, that would be for encodeURIComponent. – Patrick Evans Sep 17 '18 at 03:25
  • Possible duplicate of [What is the difference between decodeURIComponent and decodeURI?](https://stackoverflow.com/questions/747641/what-is-the-difference-between-decodeuricomponent-and-decodeuri) – Tibrogargan Sep 17 '18 at 03:28
  • @PatrickEvans yeah,I know it's two different encode/decode method.but it will give the same result.so I want to know if result is always the same. – dawn Sep 18 '18 at 01:21

1 Answers1

1

Here is the output of all the functions:

Input string: https://www.google.co.in/

Output string from encodeURI: https://www.google.co.in/

Output string from encodeURIComponent: https%3A%2F%2Fwww.google.co.in%2F

Now you decode the result of encodeURI with decodeURI and decodeURIComponent it will give you the same result. But if you decode the result of encodeURIComponent it will give you following result.

Input string: https%3A%2F%2Fwww.google.co.in%2F

Output string from decodeURI: https%3A%2F%2Fwww.google.co.in%2F

Output string from decodeURIComponent: https://www.google.co.in/

So conclusion is decodeURIComponent is designed to decode everything so it is safe to use it as per its specification means decodeURI with encodeURI and decodeURIComponent with encodeURIComponent.

Keyur Ramoliya
  • 1,900
  • 2
  • 16
  • 17
  • So the answer to the current question is *"it will give you the same result"*? Something to back this up a bit more authoritative than 'I tried once and it worked like that'. – Kaiido Sep 17 '18 at 04:30