Document.writeln() is in disuse javascript

Asked

Viewed 1,442 times

3

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

Follow the 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();  

Exit error

Invalidstateerror: An Attempt was made to use an Object that is not, or is no longer, usable

  • you want to add content without deleting the page, that’s it?

  • No, I want to show the value of string "x" on the screen just that. But ta appearing the above error, q says it is no longer in use ..

  • which browser you are using? In firefox it worked without problem.

  • In mine does not work I’m using Firefox with Firebug

  • I’ll leave a reply below, try to run the code

  • Cass persists the problem, replace new String by just "adsad"

  • oloko, seriously this, did not work ..

  • 1

    Inventing a new interpretation of the error message is not going to help. There’s probably some basic syntax error in your code, and the error you’re having is a side effect. Besides, better not invent fashion and do var x = "String é um texto dentro de aspas ";, if you are doing a conventional application.

  • I tested your code in Firefox and no errors occurred the problem should be in another script, see my answer: http://answall.com/a/121928/3635

Show 4 more comments

4 answers

4

Not the document.write or document.writeln are not in disuse, I tested the script and it does nothing and also does not present any error. So I did so:

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>

js file.:

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 the <BODY>, but soon after he finds another <body> (defined by you) and this causes a conflict (I will explain how to avoid).

Probably the mistake:

Invalidstateerror: An Attempt was made to use an Object that is not, or is no longer, usable

It is occasioned by another script and you made some mess (I’ll talk about the mistake InvalidStateError next).

What are document.write and document.writeln

The document.write is used to "write" to the document, but not to manipulate the DOM "dynamically" or asynchronously, it means that if you execute document.write inside <head> thus:

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

It would be the same as writing that:

<head>
Teste
</head>

That is to say that it will not insert inside BODY (where it is probably your goal), you should only use document.write in head if you want to enter a valid tag for <head>, for example say that you want to insert a javascript without and prevent the cache of 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>

Are just examples and not that you should really 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 the 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 at events

The document.write if used in events such as click or onload will not only add content, it will overwrite the <body></body> existing, 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 not only overwrite inserts, but after processed it needs to rebuild the structure and for that need to clean up first whole <body>, rarely will you use something this way (as 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 error Invalidstateerror

Apparently this problem occurs in Ajax requests, or in certain DOM manipulations which the object "changed" or does not exist, the situation is quite variant, but an example that the problem occurs is when we try to set a header in the XMLHttpRequest before the .open, this presents the error:

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

To correct 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 don’t see the need for that on standard or basic pages, unless you’re going to work with some XML structure within the HTML structure, however I recommend you start using HTML5, or if you still want to continue using the STRICT DTD 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 https://validator.w3.org

3

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

  • Not working aq. Syntax error appeared and could not find out. look at the error: Syntaxerror: expected Expression, got '<'

  • @Jose.Lemo try to see which of these will work, preferably do not copy the code to a file, run right here

  • Unfortunately only right running here, but in my file can not, ..

  • tries to run on Chrome, operates, it may be that your browser is in trouble, or if you are using an online IDE, this may be the problem, avoid using Ides online to make your codes, I’ve seen many problems that give headache.

  • It doesn’t work Bruno. Even vlw for the help. In fact this command is in disuse. same error: Invalidstateerror: An Attempt was made to use an Object that is not, or is no longer, usable Document.writeln(x);

2

Well I don’t know what your OS or Browser you are using because I tested it on IE 11, FF (updated), Chorme (updated), Edge and Opera (updated as well) on Windows 10 (64 bits) and they all work, so I think the problem is this, your Browser and OS.

Either way the command document.writeln is not in disuse, nor has it ever been since as far as I know there is no command similar to that in Javascript the closest would be the innerHTML but it would be element manipulation (DOM) and not written on the screen, what can occur is that it SEEMS according to the documentation that there is incompatibility of this tag with XHTML documents as described here W3C XHTML FAQ, but I honestly never witnessed that kind of thing.

Because the difference between document.writeln to the document.write is minina the only thing is that the primera gives break line as if it were a "enter" or also as a " r n" that in the translation gets:

\r = Carriage return 
\n = New line

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

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

What is in disuse but not obsolete is to start the constructor as new String(""), which are also known as "String objects" because this is more common in Java, in Javascript it is very permissive and weakly typed, so I enter disuse being more common to use the "String primitives", because there is very little ability to do this in Javascript, it serves more to create a value chain.

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" 
  • I’m using version 45.0.1 (x86 en), windows 8.1. I’m doing this example from XTI: https://www.youtube.com/watch?v=xsLSPBQJ4R4&index=26&list=PLxQNfKs8YwvEk85FbeXxDnFecAntIQdRf

1

The Document.writeln(line) command is not disused, it writes a line where the command has been specified, all xml can be overwritten if it is outside of html tags.

Note the code below taken from the site http://www.w3schools.com/jsref/met_doc_writeln.asp

Visit for more information on 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>

Browser other questions tagged

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