I have the following:
<html>
<body>
<script type="text/javascript">
document.write('Hello\nWorld')
</script>
</body>
</html>
As you probably all know, \n
doesn’t work and I have to use <br>
instead. It won’t work either if I link to an external .js
file. Here are my questions:
\n
work?<br>
even work? Shouldn’t everything that’s inside the script tags be strictly JavaScript instead of a dirty mix between HTML and JS?\n
work somehow?\t
doesn’t work either. Any other stuff that won’t work inside HTML files?document
is not defined". Same thing happens when I try from the REPL. Any ideas?When searching for similar questions, all I got was that I should use <br>
instead of \n
.
Why doesn’t
\n
work?
Because white space is just white space in HTML.
Why does
<br>
even work?
Because it is the HTML for a line break
Shouldn’t everything that’s inside the script tags be strictly JavaScript instead of a dirty mix between HTML and JS?
That’s subjective. document.write
is considered dirty by many as well.
You can always use createElement
and createTextNode
Is it possible to make
\n
work somehow?
I know
\t
doesn’t work either. Any other stuff that won’t work inside HTML files?
HTML is not plain text. Listing all the differences would be time-consuming, and out of scope for Stack Overflow. Try reading the specification.
Unrelated question (I didn’t want to open a new question just for this)
It is completely unrelated. Open a new question.