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
you want to add content without deleting the page, that’s it?
– Brumazzi DB
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 ..
– Jose.Lemo
which browser you are using? In firefox it worked without problem.
– Brumazzi DB
In mine does not work I’m using Firefox with Firebug
– Jose.Lemo
I’ll leave a reply below, try to run the code
– Brumazzi DB
Cass persists the problem, replace
new String
by just"adsad"
– Brumazzi DB
oloko, seriously this, did not work ..
– Jose.Lemo
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.– Bacco
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
– Guilherme Nascimento