0

Im doing an incredibly basic javascript exercise for school practicing for Objects. I thought the normal document.write would work but it doesn't, ive looked many places but a lot of it is just for the console. The error is

Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.

If anyone can help that would be great, sorry if its really easy

Here is my code

var oPerson = {
  firstName: 'John',
  lastName: 'Travis',
  gender: 'Male',
  age: 22,
  district: 'Northshore',
  hairColor: 'Brown',
  hairLength: 'Short',
  height: '6\'11"',
  weight: '74kg',
  martialStatus: 'Engaged'
}

document.write(oPerson);
document.write(oPerson.district);

oPerson.resident = "yes";

document.write(oPerson);
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Exercise - Personal Info</title>
  <script src="practice9JS.js" type="text/javascript"></script>
</head>

<body>
</body>

</html>

7
  • 2
    your code is working Commented Oct 16, 2017 at 21:28
  • It needs to display the object contents as whole as well Commented Oct 16, 2017 at 21:29
  • 1
    So what was all the talk about Failed to execute 'write' on 'Document'? Is the issue an error, or incorrect output? And by incorrect, I mean "not what you expected" because what is printed is correct for the code you wrote. Commented Oct 16, 2017 at 21:31
  • Using the dev tools and putting breakpoints on the statements it shows with that error. The code i wrote should display the object as a whole Commented Oct 16, 2017 at 21:32
  • Your code doesn't present the object because you need to use JSON.stringify to convert to object into a string - document.write(JSON.stringify(oPerson)) Commented Oct 16, 2017 at 21:34

3 Answers 3

1

Your document.write() calls are exectuting as the HTML is being read. By the time the DOM loads, your messages are no longer visible. Try this:

setTimeout(() => document.write(oPerson.lastName), 1000);
Sign up to request clarification or add additional context in comments.

5 Comments

It works displaying it to the console but im trying to display it on the site
@Dale well if you can't adjust that, what are you doing programming?
Its just a class exercise, when displaying an object, for these purposes it needs to be on the screen. Otherwise i would
Printing a specific part of the object works usually without the timer, the timer doesn't make a difference when wanting to display the object as a whole
Then I would try appending the object to the HTML as text. {document.getElementsByTagName('body').innerHTML(oPerson.lastName);}
0

It's not advised to use document.write, becasue it have to be executed before loading the page. Also it's executed in HTML place exactly where was added. So in case of your code it will be added to head of the page, not the body. If you wish to just add something to HTML, you can do it withdocument.appendChild`. Where you create new element, provide some necessary info and append it to the body.

e.g. https://codepen.io/msamsel/pen/zEMZXX?editors=1010

function printToBody( str ) {
  var par = document.createElement( 'p' );
  par.innerText = str;
  document.getElementsByTagName( 'body' )[0].appendChild( par );
}

var oPerson = {
  firstName: 'John',
  lastName: 'Travis',
  age: 22
}

printToBody( oPerson.firstName )
printToBody( oPerson.lastName )
printToBody( oPerson.age )

If you really want to use document write, then move script from head to body. Then document.write should execute there.

Comments

0

I'm not sure what you try to do, but if simply post content of object without any formatting, this could work for you.

var oPerson = {
  firstName: 'John',
  lastName: 'Travis',
  gender: 'Male',
  age: 22,
  district: 'Northshore',
  hairColor: 'Brown',
  hairLength: 'Short',
  height: '6\'11"',
  weight: '74kg',
  martialStatus: 'Engaged'
}
var app = document.getElementById('app');
app.innerHTML = JSON.stringify(oPerson);
<div id="app">
</div>

1 Comment

Good enough haha. Don't want the speech marks and curly braces but its ok

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.