Javascript decoding special charcters

74 Views Asked by At

I'm receiving from my server a string with " char as ". I would like to display this string correctly without any coded characters

So I try to use decodeURI or unescape function as follows:

decodeURI(""")
unescape(""")

buy still the output stays coded

"""

Any clue why?

Thanks!

4

There are 4 best solutions below

0
On

decodeURI() and unescape() are used to decode URI content, not HTML. I think you are mixing your technologies.

If jquery is available then these might help you: Javascript decoding html entities How to decode HTML entities using jQuery?

0
On

use Jquery

$("myel").html(""");

will result as proper string on the screen, while the source is "\

2
On

If you want the javascript-only version to decode that, use this:

function decodingFunctionVer(str) {
    var tmp = document.createElement("div");
    tmp.innerHTML = str;
    return tmp.textContent;
}

With jsFiddle demo.

0
On

Solved:

function decodeText(encodedText){
        var div = document.createElement('div');
        div.innerHTML = encodedText;
        var decoded = div.firstChild.nodeValue;
        return decoded;
    }

I don't like this solution becuse it includes creating a redundent element but it works.

Thanks for the help