1

I've been trying decode text, so I can take unescape JSON encoding. But I can't get to work when the variable value is innerHTML but it works when I hardcode the same string value to the variable.

innerHTML Example (I need help with)

<html>
<body>

<p id="uri">C:\\Users\\User\\Documents\\Fax</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
  var uri = document.getElementById("uri").innerText;
  var dec = decodeURI(uri.toString());
  var res = "Decoded URI: " + dec;
  document.getElementById("demo").innerHTML = res;
}
</script>

</body>
</html>

Expected Result: Decoded URI: C:\Users\User\Documents\Fax

Working example with a hardcoded value

    <html>
    <body>

    <p id="uri">Decode</p>

    <button onclick="myFunction()">Try it</button>

    <p id="demo"></p>

    <script>
    function myFunction() {
      var uri = "C:\\Users\\User\\Documents\\Fax";
      var dec = decodeURI(uri.toString());
      var res = "Decoded URI: " + dec;
      document.getElementById("demo").innerHTML = res;
    }
    </script>

    </body>
    </html>

Actual Result: Decoded URI: C:\Users\User\Documents\Fax

What am I doing wrong?

DN0300
  • 864
  • 2
  • 12
  • 27

2 Answers2

2

The line:

var uri = "C:\\Users\\User\\Documents\\Fax";

actually sets uri to C:\Users\User\Documents\Fax. Since you are simply creating a string with the backslashes escaped.

The line:

var uri = document.getElementById("uri").innerText;

sets uri to C:\\Users\\User\\Documents\\Fax with two backslashes in each position.

If you want the string solution to behave the same, you need to escape the backslashes again when creating the string. (i.e. var uri = "C:\\\\Users\\\\User\\\\Documents\\\\Fax";)


To fix your first version to work as the second currently does, you can use a RegEx replacement to change C:\\Users\\User\\Documents\\Fax to C:\Users\User\Documents\Fax (or if it is possible, simply change the html element to contain C:\Users\User\Documents\Fax).

RegEx solution:

<html>
<body>

<p id="uri">C:\\Users\\User\\Documents\\Fax</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
  var uri = document.getElementById("uri").innerText.replace(/\\\\/g,"\\");
  console.log("uri = " + uri);
  var dec = decodeURI(uri.toString());
  var res = "Decoded URI: " + dec;
  document.getElementById("demo").innerHTML = res;
}
</script>

</body>
</html>

To parse all escaped characters (\\, \n, \r, \t, ...):

<html>
<body>

<p id="uri">C:\\Users\\User\\Documents\\Fax</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
  var uri = document.getElementById("uri").innerText;
  console.log("uri = " + uri);

  uri = JSON.parse(`"${uri}"`);
  console.log("parsed uri = " + uri);

  var dec = decodeURI(uri.toString());
  var res = "Decoded URI: " + dec;
  document.getElementById("demo").innerHTML = res;
}
</script>

</body>
</html>
DenverCoder1
  • 2,253
  • 1
  • 10
  • 23
  • I see what you are doing with replace although that works for "\\" usecase, decodeURI still ignores decoding \n, \r other JSON Espace characters that it adds to my strings. – DN0300 May 12 '20 at 18:18
  • @DN0300 It seems like you just want to remove all backslash escaping from the variable. In that case, `JSON.parse` should be all you need. This should work: `JSON.parse(\`"${document.getElementById("uri").innerText}"\`);` (or see full example added at end of my answer). – DenverCoder1 May 12 '20 at 18:50
  • @DN0300 Like lakshitha said, `decodeURI` is for hex encoded strings like `"%5C%20%C3"` and not backslash escaped strings, so it doesn't do anything to your string. – DenverCoder1 May 12 '20 at 18:58
  • Is there something similar to json.Parse I can use for innerHtml? – DN0300 May 13 '20 at 12:46
0

Use encoded URI to decode, if not we couldn't see the difference.

<p id="uri">my%20test.asp?name=st%C3%A5le&car=saab</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
    function myFunction() {
        var uri = document.getElementById("uri").innerText;
        var dec = decodeURI(uri.toString());
        var res = "Decoded URI: " + dec;
        document.getElementById("demo").innerHTML = res;
    }
</script>