4

When I receive html text by ajax in asp.net application it looks like:

<span%20style='color:green;font-weight:bold'>%20Text%20Msg</span>

how is it possible in javascript decode that text to normal html?

<span style='color:green;font-weight:bold'> Text Msg </span>

Thanks!

ihorko
  • 6,855
  • 25
  • 77
  • 116
  • 3
    There is no technical reason it should look like that - especially since it looks like some strange mixture of HTML and URL encoding which neither does JSON require. You should check you server-side script and find out why it's being encoded and turn it off. – RoToRa Jan 14 '11 at 15:52
  • Are you using an XSLT to generate the markup? What is generating this code? – epascarello Jan 14 '11 at 16:00
  • It looks like the string is double encoded: first Html Encoded and then URL encoded. Why is that so? – Salman A Jan 14 '11 at 16:00

2 Answers2

1

Nice function here that does it for you - http://phpjs.org/functions/htmlspecialchars_decode:427

Matt Asbury
  • 5,644
  • 2
  • 21
  • 29
0

You are probably best suited with finding a server side solution as already mentioned in the comments, since this seems like a server side problem.

If you for some reason wish to do this client side anyway, here is a solution:

var str = "&lt;span%20style='color:green;font-weight:bold'&gt;%20Text%20Msg&lt;/span&gt;";
var fixedStr = decodeURIComponent(str).replace(/&lt;/g,'<').replace(/&gt;/g,'>');
Martin Jespersen
  • 25,743
  • 8
  • 56
  • 68