Javascript preformatted text with cross-browser line breaks -


i have preformatted strings line-breaks , multi-spaces , want append them text node.

<pre id="bar"></pre>  <script>    var string = "preformatted"                 + "\n"  // \r, \r\n, \n\r or else?                 + "multispace     string";    var text = document.createtextnode(string);    document.getelementbyid('bar').appendchild(text); </script> 

i tried adopt line breaker:

  • \n breaks lines in browsers, in ie (i'm testing on 7) becomes space
  • \r breaks lines in ie
  • \r\n works in browser in ie space @ beginning of second line horror
  • \n\r ok in all, in ie space @ end of first line inacceptable layout.

i can't use <br> , innerhtml because ie collapses multi-spaces.
jquery .text(string) has same behavior of .appendchild(createtextnode(string))

how can insert cross-browser line breaks?
eventually, how can detect if browser supports \n or \r ?

this seemed work in browsers tested (safari, opera, chrome, firefox, ie7, ie8, ie9):

http://jsfiddle.net/4bq5q/1/

code:

var textarea = document.createelement("textarea"); textarea.value = "\n"; var eol = textarea.value.replace(/\r\n/, "\r");  var string = "preformatted" + eol + "multispace     string";  var text = document.createtextnode(string); document.getelementbyid('bar').appendchild(text);​ 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -