0

I am trying to use newline ("\n") inside a window.open().document in javascript. which is not working as expected . "\" the back slash basically causing the problem where its kind of acting as an break to my code . i have to have this part of code to get the number of counts of newline and also to replace the unnecessary newlines . how can i make "\" to be considered as string?

I have tried salutions such as adding four back slash (\\) by referring to the solution but it had no change Can't escape the backslash with regex?

printWindow=window.open("","","location=no,menubar=no,directories=no,scrollbars=yes,toolbar=no,resizable=yes",false);

printWindow.document.write('var commentCharLimit = 6000;');
printWindow.document.write('for(var x = 0; x < textareas.length; x++)');
printWindow.document.write('{');
printWindow.document.write('if(textareas[x].className.indexOf("lh-advisor-comment-input") > -1){');
printWindow.document.write('var textareaContent = textareas[x].value;');

// this is where my code is getting failed because of the "\" used

printWindow.document.write('textareaContent = textareaContent.replace(/(\r\n|\n|\r)/gm,"");');
printWindow.document.write('var truncatedTextareaContent = textareaContent.substring(0, commentCharLimit);');
printWindow.document.write('textareas[x].value = truncatedTextareaContent;');

printWindow.document.write('var maxrows=300; ');

//printWindow.document.write(' textareas[x].rows=50;');
printWindow.document.write(' var cols=textareas[x].cols; ');

// also here

printWindow.document.write('var arraytxt=truncatedTextareaContent.split("\\n");');

printWindow.document.write('var rows=arraytxt.length; ');
printWindow.document.write('for (i=0;i<arraytxt.length;i++) ');
printWindow.document.write('rows+=parseInt(arraytxt[i].length/cols)');
printWindow.document.write(' if (rows>maxrows) textareas[x].rows=maxrows;');
printWindow.document.write(' else textareas[x].rows=rows;');

printWindow.document.write('}');
printWindow.document.write('}');

I expect the output to consider the "/" and execute it if i try the same code outside the printwindow.document it works fine .

Divakar R
  • 81
  • 5
  • 12

2 Answers2

1

needs to be escaped for it to be ignored. The following works when I run it in the console, I see \r\n|\n|\r in the output.

printWindow=window.open("","","location=no,menubar=no,directories=no,scrollbars=yes,toolbar=no,resizable=yes",false);

printWindow.document.write('var commentCharLimit = 6000;');
printWindow.document.write('for(var x = 0; x < textareas.length; x++)');
printWindow.document.write('{');
printWindow.document.write('if(textareas[x].className.indexOf("lh-advisor-comment-input") > -1){');
printWindow.document.write('var textareaContent = textareas[x].value;');

printWindow.document.write('textareaContent = textareaContent.replace(/(\\r\\n|\\n|\\r)/gm,"");');
printWindow.document.write('var truncatedTextareaContent = textareaContent.substring(0, commentCharLimit);');
printWindow.document.write('textareas[x].value = truncatedTextareaContent;');

printWindow.document.write('var maxrows=300; ');

//printWindow.document.write(' textareas[x].rows=50;');
printWindow.document.write(' var cols=textareas[x].cols; ');

// also here

printWindow.document.write('var arraytxt=truncatedTextareaContent.split("\\\\n");');

printWindow.document.write('var rows=arraytxt.length; ');
printWindow.document.write('for (i=0;i<arraytxt.length;i++) ');
printWindow.document.write('rows+=parseInt(arraytxt[i].length/cols)');
printWindow.document.write(' if (rows>maxrows) textareas[x].rows=maxrows;');
printWindow.document.write(' else textareas[x].rows=rows;');

printWindow.document.write('}');
printWindow.document.write('}');
epascarello
  • 204,599
  • 20
  • 195
  • 236
0

Try to use <br> instead. It worked fine.

see working code

Kareem Dabbeet
  • 3,838
  • 3
  • 16
  • 34