1

I have a parameter in my grails view which I am passing to javascript code

<g:if test="${replacedString}">
    ${replacedString}
    <g:set var="replacedString" value="${replacedString}"/>
</g:if>
<script>
   var mydata = decodeURIComponent(${replacedString});
   console.log(mydata);
</script>

The replacedString value is being passed as an encoded string i.e: " appears as &quot;. Due to this I have the method

decodeURIComponent

This is the string I'm trying to decode:

[{&quot;description&quot;:&quot;test1 &quot;,&quot;filenameAndPath&quot;:&quot;test1.pdf&quot;},{&quot;description&quot;:&quot;test file&quot;,&quot;filenameAndPath&quot;:&quot;test copy.pdf&quot;},{&quot;description&quot;:&quot;&quot;,&quot;filenameAndPath&quot;:&quot;&quot;},{&quot;description&quot;:&quot;&quot;,&quot;filenameAndPath&quot;:&quot;&quot;}]

This block of code keeps throwing the error Syntax error: Unexpected token &.
Am I missing something obvious ?

shanwar
  • 319
  • 1
  • 2
  • 19

1 Answers1

1

This is HTML encoded.

use this trick from here:-

var encoded = "[{&quot;description&quot;:&quot;test1 &quot;,&quot;filenameAndPath&quot;:&quot;test1.pdf&quot;},{&quot;description&quot;:&quot;test file&quot;,&quot;filenameAndPath&quot;:&quot;test copy.pdf&quot;},{&quot;description&quot;:&quot;&quot;,&quot;filenameAndPath&quot;:&quot;&quot;},{&quot;description&quot;:&quot;&quot;,&quot;filenameAndPath&quot;:&quot;&quot;}]"

var elem = document.createElement('textarea');
elem.innerHTML = encoded;
var decoded = elem.value;

console.log(decoded);

See here for the differences in HTML and URL Encoding

Community
  • 1
  • 1
BenG
  • 14,826
  • 5
  • 45
  • 60