Looking at the specification material Ecmascript 2018 no reference to the function, but in the documentation of the WHATWG.org the function remains active. I believe it is in disuse, because with so many more advanced features emerged since the specification in which the .writeln
was implemented (DOM Level 2, about 16 years ago), it became obsolete and without much (or no) usefulness.
The function document.writeln
inserts a line break \n
at the end of the string. Since HTML treats line breaking in code as a space, you will view one text after the other separated by a space:
document.writeln("texto1");
document.writeln("texto2");
However, if you put it within a tag <pre>
line break is applied as the content of the tag <pre>
is rendered in the browser as is in the source code:
document.write("<pre>");
document.writeln("Nova Linha1");
document.writeln("Nova Linha2");
document.write("</pre>");
Making a replace us \n
added by .writeln
for <br>
to break the line in HTML:
// Usando write
document.write("Mesma Linha1");
document.write("Mesma Linha2");
// Quebra de linha
document.write("<br/>");
// Usando o writeln
document.write("<div>");
document.writeln("Nova Linha1");
document.write("Nova Linha2");
document.write("</div>");
var html = document.querySelector("div").innerHTML;
document.querySelector("div").innerHTML = html.replace(/\n/g,"<br>");
Yes, it would work this way, but writeln should be independent like other programming language the writeln should make sense by itself not depending on another element to be able to work, so it would not need to use it but another element ?.
– Leandro Nascimento