Decode HTML in javascript

porlhews

Member
Hi all,

I have a textarea element which is populated by an initial user and saved with escaped htmlspecialchars, for example:

Code:
<p>Text here</p>

I have a button element, which I want to open a pop out browser tab and displays the text area content as rendered HTML. The following code sucessfully opens the new tab and populates it with the content of the textarea:

JavaScript:
var text = Fabrik.getBlock("form_1").elements.get("cor___textarea").get("value");

var myWindow = window.open( "", "MsgWindow", "width=200,height=100");


var open = "<!doctype html><html><body>"; 
var ces = open.concat(text);

var close = "</html></body>"; 

var out = ces.concat(close);


myWindow.document.write(out);


The popup window sucessfully opens; however, the content is rendered as text like this:

Code:
<p>Text here</p>

I want it to render as valid HTML code rather than print the HTML.

After much googling, I understand that I need a javascript equivalent of htmlspecialchars_decode. I've found several offerings where people have resolved this issue, but just cannot get any of them to work.

My latest attempt is below:

JavaScript:
var text = Fabrik.getBlock("form_19").elements.get("correspondence_system___message_history").get("value");

function htmlDecode(input){
  var e = document.createElement('div');
  e.innerHTML = input;
  return e.childNodes[0].nodeValue;
}


var mid = htmlDecode(text);


var open = "<!doctype html><html><body>"; 
var ces = open.concat(mid);

var close = "</html></body>"; 

var out = ces.concat(close);

var myWindow = window.open( "", "MsgWindow", "width=200,height=100");
myWindow.document.write(out);


Please can you help?

Thanks in advance :)
 
Back
Top