9

I have a string coming from a XML (which I can't edit) and I'd like to print it trough an alert in javascript.

Example of my string:

This is à string

And I need to print in an alert:

This is à string

is there a js html decode?

3
  • You need to show a little code which demonstrates how this values makes its way from the XML into the JS. Commented Feb 18, 2013 at 13:13
  • I got it from XmlReader in C# Commented Feb 18, 2013 at 13:14
  • if you using jquery this question tells you how stackoverflow.com/questions/1147359/… Commented Feb 18, 2013 at 13:14

3 Answers 3

13

you could put the string in a dom element and read it out again, even without jquery: https://stackoverflow.com/a/3700369/1986499

Edit by recent demand to include some code from another SO answer:

var div = document.createElement('div');
div.innerHTML = encoded;
var decoded = div.firstChild.nodeValue;
Sign up to request clarification or add additional context in comments.

1 Comment

Just because the question is a duplicate doesn't mean you should post a link-only answer.
12
var encoded = "This is à string";
var decoded = $("<div/>").html(encoded).text();
alert(decoded);

1 Comment

What this answer fails to mention is that, despite the question not being tagged with it, you need the jQuery library to use this answer. Without jQuery you'll be greeted with a lovely DOMException error.
2

I'm just a tiny bit late, but just in case anyone else finds this via Google (like I did), I thought I'd improve upon Imperative's answer.

function showbullet() {
  var tempelement = document.createElement('div');
  tempelement.innerHTML = "&bull;";
  alert("Here, have a bullet!\n" + tempelement.innerHTML);
}
showbullet();

I've tested this and confirmed it works in Chrome/43.0.2357.130 m; Firefox/32.0.1; Internet Explorer/9.0.8112.16421. There's no need to go mucking about with nodeValue's and what not; the entity will be replaced with it's associated character as soon as the assignment is complete. (Note, however, that doing alert(tempelement.innerHTML="&bull;"); does not work in any of the browsers I tested!)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.