I'm getting html data from a database which has been sanitised.
Basically what I'm getting is something like this:
<div class="someclass"><blockquote> <p>something here.</p> </blockquote>
And so on. So if I try to display it, it is displaying as
<div class="someclass"><blockquote> <p>something here</p> </blockquote>
What I want is to convert it to proper html before displaying, so that the content displays properly, without the tags.
What's the easiest way to do this using javascript?
Just want to note here that I'm working with in Adobe AIR. So I don't have any alternatives.
You could create an element, assign the encoded HTML to its innerHTML and retrieve the nodeValue from the text node created on the insertion.
function htmlDecode(input){ var e = document.createElement('div'); e.innerHTML = input; return e.childNodes[0].nodeValue; } htmlDecode('<div class="someclass"><blockquote> <p>" ' + 'something" here.</p>Q</blockquote>') // returns : // "<div class="someclass"><blockquote> <p>"something" here.</p>Q</blockquote>"
Note that this method should work with all the HTML Character Entities.
This could help in a snap:
String.prototype.deentitize = function() { var ret = this.replace(/>/g, '>'); ret = ret.replace(/</g, '<'); ret = ret.replace(/"/g, '"'); ret = ret.replace(/'/g, "'"); ret = ret.replace(/&/g, '&'); return ret; };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With