html entities in a javascript alert?

删除回忆录丶 提交于 2019-11-28 12:24:17
var encoded = "This is à string";
var decoded = $("<div/>").html(encoded).text();
alert(decoded);
Imperative

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;
DimeCadmium

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!)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!