简体   繁体   中英

Javascript decoding special charcters

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!

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?

use Jquery

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

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM