1

I dont know why I cannot make a line break in my javascript. Here is the code:

document.write('It\'s ' + n + ',\n'+'SORRY, WE \'RE CLOSED');

Thanks,

raina77ow
  • 91,589
  • 12
  • 180
  • 210
Duy Tran
  • 15
  • 2

2 Answers2

2

That function will output HTML, so create line breaks by adding <br />, not \n.

document.write('It\'s ' + n + ',<br />SORRY, WE \'RE CLOSED');

http://jsfiddle.net/cuLdnjhu/

However, you shouldn't really be using document.write(): Why is document.write considered a "bad practice"?

A better option is document.body.innerHTML or document.createElement().

https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement

https://developer.mozilla.org/en-US/docs/Web/API/Document

Community
  • 1
  • 1
Alvin Pascoe
  • 1,189
  • 7
  • 5
0

Works for me.

<span style="white-space:pre">
<script>
var n = 10;
document.write('It\'s ' + n + ',\n'+'SORRY, WE\'RE CLOSED');
</script>
</span>
Alohci
  • 70,004
  • 12
  • 103
  • 143