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:
\nbreaks lines in browsers, in ie (i'm testing on 7) becomes space\rbreaks lines in ie\r\nworks in browser in ie space @ beginning of second line horror\n\rok 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):
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
Post a Comment