1

There is some content that is fetched from the server and is shown on a new page in the browser. For example of the content is

"Apple & Orange"

But when it is rendered in web page code for which is

function writeConsole(content,idx) {
     top.consoleRef=window.open('','myconsole' + idx,
        ',menubar=0'
       +',toolbar=1'
       +',status=0'
       +',scrollbars=1'
       +',resizable=1');
     top.consoleRef.document.writeln(
      '<html><head><title> Generated Content </title></head>'
       +'<body bgcolor=white onLoad="self.focus()">'
       + '<textarea rows="500" cols="500">'
       + content
       + '</textarea>'
       +'</body></html>'
     );
     top.consoleRef.document.close();
}

it is getting rendered as

"Apple & Orange"

Why is this happening? I need to display the String at it is. What changes should I make to achieve this? I need to keep those escaped characters as it is. Any help or suggestions are appreciated.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • Have added complete problem and solution -> http://opensourceforgeeks.blogspot.in/2014/06/open-new-web-page-to-show-content-using.html – Aniket Thakur Aug 05 '14 at 16:36

1 Answers1

1

document.writeln expects its input to be HTML so your content is getting treated as so and this includes transforming entity references such as &amp; or &lt; into & and <. After all, you want <title> to be treated as a tag instead of as some < symbols, right?

There are multiple ways to fix your problem. One possibility you can do is to insert the text content in a separate pass, using DOM methods. This way you don't have to deal with html escaping issues.

top.consoleRef.document.close();

var doc = top.consoleRef.document;
var textarea = doc.getElementsByTagName('textarea')[0];
textarea.value = "Apple &amp; Orange";

Alternatively, you can html escape the content string to turn it into "Apple &amp;amp; Orange", which is how you can write "Apple &amp; Orange" in the raw html document.

Community
  • 1
  • 1
hugomg
  • 68,213
  • 24
  • 160
  • 246