document.writeln () is deprecated javascript

1

When document.writeln() used to display text in html is deprecated? Does anyone know of another way to display a string on the screen?

Follow xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">     
<head> 
    <title> JavaScript Objeto String</title>    

    <script type = "text/javascript" src = "js/ObjetoString.js"> </script> 

</head>    
<body>    

</body>
</html>

Follow the javascript

var x = new String("Texto dentro de aspas é string");

document.writeln();  

Output error

  

InvalidStateError: An attempt was made to use an object that is not, or no longer, usable

    
asked by anonymous 04.04.2016 / 23:53

4 answers

2

<script>
    var imprime_1 = function(){
        document.writeln("Valor");
    }
    var imprime_2 = function(){
        var x = new String("var String");
        document.writeln(x);
    }
</script>
<form>
  <input type="button" value='ação1' onclick="imprime_1()">
  <input type="button" value='ação2' onclick="imprime_2()">
</form>
    
05.04.2016 / 00:32
2

Not the document.write or document.writeln are not deprecated, I have tried the script and it does nothing and it does not show any error. So I did like this:

html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>JavaScript Objeto String</title>
        <script type="text/javascript" src="arquivo.js"></script>
    </head>
    <body>
        <p>test</p>
    </body>
</html>

file.js:

var x = new String("Texto dentro de aspas é string");
document.writeln(x);

Only in IE11 an error appears:

  

HTML1514: Extra "< body >" tag found. Only one "< body >" tag should exist per document.

But this error is not Javascript but HTML, when using document.write before the body without a valid tag for <head> IE automatically creates <BODY> , but soon after it finds another <body> (defined by you) and this causes a conflict (I'll explain how to avoid it).

Probably the error:

  

InvalidStateError: An attempt was made to use an object that is not, or no longer, usable

It is caused by another script and you have made some confusion (I will mention the error InvalidStateError below).

What are document.write and document.writeln

The document.write is used to "write" in the document, but not to manipulate the DOM "dynamically" or even asynchronously, this means that if you execute document.write within <head> like this: p>

<head>
<script>
document.write("Teste");
</script>
</head>

It would be the same as writing this:

<head>
Teste
</head>

In other words, this will not insert into BODY (where it is probably your goal), you should only use document.write in head if you want to insert a valid tag to <head> , say say you want to insert a javascript without and prevent caching it, you can do so:

<head>
<script>
var time = new Date().getTime();
document.write('<script src="file.js?_=' + time + '"></script>');
</script>
</head>

Or you want to show the date in the title:

<head>
<script>
var minhaData = new Date();
document.write('<title>' + minhaData + '</title>');
</script>
</head>
  

Just examples and not really what you should use

What matters is to understand that within <head> there can only be valid tags, even if the content is inserted by document.write .

The document.writeln is almost identical to document.write , the only difference is that it generates a line break at the end of the inserted string, it would be almost the same as:

document.write("Foo bar\n");

document.write in events

The document.write if used in events such as click or onload will not only add content, it will overwrite existing <body></body> , for example:

document.getElementById("testar").onclick = function() {
    document.write("Olá mundo!");
};
<p>test</p>

<button id="testar">Testar</button>

The document.write out of events and before the document is processed does not overwrite it just inserts, but after processing it needs to rebuild the structure and for this it needs to first clean all <body> , rarely will you use something this way in the example above).

How to insert content into the page dynamically

To avoid cleaning the body you have several options, such as .innerHTML , .appendChild , .insertBefore

An example with innerHTML :

var conteudo = document.getElementById("conteudo");

document.getElementById("testar").onclick = function() {
    conteudo.innerHTML = "Olá mundo! Data atual: " + (new Date());
};
<p>test</p>

<div id="conteudo"></div>

<button id="testar">Testar</button>

The InvalidStateError error

It seems that this problem occurs in Ajax requests, or in certain DOM manipulations that the object "has changed" or does not exist, the situation is very variant, but an example that the problem occurs is when we try to set a header in% before XMLHttpRequest , this displays the error:

var xhr = new XMLHttpRequest();
xhr.setRequestHeader( 'X-Requested-With', 'XMLHttpRequest' );

To fix it do this:

var xhr = new XMLHttpRequest();
xhr.open('GET', 'pagina.html', true);
xhr.setRequestHeader( 'X-Requested-With', 'XMLHttpRequest' );

I recommend that you study and learn how events / callbacks work, I recommend these two answers:

XHTML and HTML5

I noticed that your code uses XHTML, I do not see any need for this in standard or basic pages, unless you work with some XML structure inside the HTML structure, however I recommend starting HTML5, or if you want but still continue using the DTD STRICT then use HTML4.01, in most cases you will not use XHTML.

An example of HTML4.01 with strict:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <title>Teste</title>
</head>
<body>
    <p>Olá mundo</p>
</body>
</html>

HTML5:

<!DOCTYPE html>
<html>
<head>
    <title>Teste</title>
</head>
<body>
    <p>Olá mundo</p>
</body>
</html>

And to validate your HTML you can use this link link

    
06.04.2016 / 18:33
1

Well, I do not know what your OS or Browser you are using because I tested it in IE 11, FF , Chorme , Edge and Opera (updated too) on Windows 10 (64 bits) and all work, so I believe the problem is this, your Browser and OS.

Anyway, the command document.writeln is not deprecated, nor was it ever since, as far as I know, there is no such command in JavaScript, the next would be innerHTML but it would be element manipulation (DOM) and not written on the screen, what may occur is that it appears that according to the documentation there is incompatibility of this tag with XHTML documents as described here W3C XHTML FAQ , but honestly I've never witnessed this kind of thing.

Even though the difference from document.writeln to document.write is minina the only thing is that the first one gives a line break as if it were an "enter" or also as a "\ r \ n" in the translation :

\r = Carriage return 
\n = New line

And that can vary its interpretation for each OS that would read this command, because depending on the combination can add or miss "enters":

  • Mac: \ r
  • Linux / Unix: \ n
  • Windows: \ r \ n

What is deprecated but not obsolete is to start the constructor as new String("") , which are also known as " String objects " since this is more common in Java, in JavaScript it is quite permissible and poorly typed, so it is not usual to use "String primitives , because there is very little usefulness to be doing this in JavaScript, it is more useful for aramzenar a string of values.

var var1 = "Texto 1";
var1.prop = "Texto 2";
alert(var1.prop); // undefined

var var1 = new String("Texto 1");
var1.prop = "Texto 2";
alert(var1.prop); // "Texto 2" 
    
05.04.2016 / 02:15
0

The command document.writeln (line), is not deprecated, it writes a line where the command was specified, all the xml can be overwritten if it is outside of the html tags.

Look at the code below from link

Access to more information about document.writeln ().

<body>

 <p>Note that write() does NOT add a new line after each statement:</p>

 <pre>
 <script>
  document.write("Hello World!");
  document.write("Have a nice day!");
 </script>
 </pre>

 <p>Note that writeln() add a new line after each statement:</p>

 <pre>
 <script>
  document.writeln("Hello World!");
  document.writeln("Have a nice day!");
 </script>
 </pre>

</body>
    
06.04.2016 / 06:10