writeln in Javascript is in disuse or not?

Asked

Viewed 295 times

1

I didn’t find any documentation saying that writeln is obsolete, but he was supposed to write in a new line, but it’s not what’s happening, it works the same as the write and stays in one line only. Anyone knows if it still works in Ecmascript 2018 specifications?

// Usando write
document.write("Mesma Linha1");
document.write("Mesma Linha2");

// Quebra de linha
document.write("<br/>");

// Usando o writeln
document.writeln("Nova Linha1");
document.writeln("Nova Linha2");

2 answers

2

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>");

  • 1

    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 ?.

1

document.writeln writes to a new line. Writes to a new line in HTML.

If you check the DOM of your page after executing the commands, it will look like this:

<body>
    Mesma Linha1Mesma Linha2<br>
    Nova Linha1
    Nova Linha2
</body>

But you can’t see this line break on your page, because HTML ignores line breaks - so much so that you’ll probably use line breaks in HTML to keep your code organized without it affecting your page.

If you want the line breaks to be interpreted literally in HTML, try putting your code inside the tag <pre>

// Usando write
document.write("<pre>");
document.write("Mesma Linha1");
document.write("Mesma Linha2");

// Quebra de linha
document.write("<br/>");

// Usando o writeln
document.writeln("Nova Linha1");
document.writeln("Nova Linha2");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.