-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,968
  • 16
  • 57
  • 91

2 Answers2

4

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
  • 510,633
  • 85
  • 743
  • 889
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,604
  • 1
  • 10
  • 10