-3

I have the following code:

var msg = "Research & Analysis",
    decoder = decodeURIComponent(msg);

console.log(msg);
console.log(decoder);

Can someone please help me understand why the decodeURIComponent(msg); is not actually decoding the & in the string?

I am expecting it to return something like Research & Analysis instead of Research & Analysis

gespinha
  • 7,370
  • 13
  • 51
  • 84

2 Answers2

3

decodeURIComponent decodes, well, URI components, meaning URL-encoded strings. & is an HTML entity, not URL-encoding. decodeURIComponent has nothing to do with HTML encoding.

deceze
  • 491,798
  • 79
  • 706
  • 853
0

I would like to refer you to the documentation of the encode and decode function:

http://www.w3schools.com/jsref/jsref_encodeuricomponent.asp http://www.w3schools.com/jsref/jsref_decodeuricomponent.asp

As you can probably understand now, encoding and decoding of an uri has nothing to do with & but is something different.

For the real answer I'd like to refer you to another question + answer here: Decode & back to & in JavaScript

Community
  • 1
  • 1
Martijn Smidt
  • 1,554
  • 1
  • 9
  • 10