0

I have got a little problem.

I get a String back in a JSON response which has control characters(\n) in it.

"ClData":" stream TV and films online. O\u0027Dwyer is accused of being the    administrator of the site. \n\nStudent O\u0027Dwyer was arrested on 23 May and spent the night in custody, before his aunt posted bail of £3,000."

Now when I use

console.log(root.CatLong.ClData)

I get formatted text back in the console.

But if i use

content.innerHTML = root.CatLong.ClData

It displays it without the "\n" but completly unformatted.

Is the error on my side or is .innerHTML just not capable of interpreting control characters.

Thanks in advance Max

  • Newlines probably get folded into whitespace. Do you get "O Dwyer" or "ODwyer" there? – AKX Mar 14 '13 at 12:37
  • Execute this code in the console, you will see the result in place of the question title : `document.getElementById('question-header').innerHTML = " stream TV and films online. O\u0027Dwyer is accused of being the administrator of the site. \n\nStudent O\u0027Dwyer was arrested on 23 May and spent the night in custody, before his aunt posted bail of £3,000.";`. Nothing seems to be wrong, maybe you are using an incompatible charset for your page. –  Mar 14 '13 at 12:42
  • Fixed it by replacing the /n with
    thanks for your help :)
    –  Mar 14 '13 at 12:46

2 Answers2

2

As far as the browser is concerned the "\n" is just white space and will get ignored. Try enclosing the text in a pre tag to tell the browser it is pre-formatted.

content.innerHTML = "<pre>" + root.CatLong.ClData + "</pre>";
chris
  • 96
  • 5
0

I have to replace the \n with <br> like this one guy who deleted his answer pointed out

content.innerHTML = root.CatLong.ClData.replace((/\n/g,"<br>");
Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80