Difference between document.write and document.body.innerHTML

Could somebody help me understand why I'm getting different results when using

<body>
  <script>
    document.write(fnMyFunction());
  </script>
</body>

versus

<body>
</body>
<script>
  document.body.innerHTML=fnMyFunction();
</script>

Even a link to an explanation would help a lot.
LVL 2
etd_onlineguysAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ian GoughCommented:
Hi!
This should claify things,
Ian
0
Ian GoughCommented:
http://www.wait-till-i.com/index.php?p=104

Sorry with the link it's better!
Ian
0
etd_onlineguysAuthor Commented:
The problem I'm having is that a function running subsequent to the results of fnMyFunction() is erroring when it tries to find HTML objects or properties created with the innerHTML assignment.  It works fine with the document.write(fnMyFunction()).  Since document.write(fnMyFunction()) is the bigger no-no of the two, I'd like to understand why these two don't behave identically.  
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Ian GoughCommented:
I'm guessing it's as it's more vague as the link suggests.
"document.write What this does is write out content to the HTML document, inside the body of the document and merrily mixed with the markup" (http://www.wait-till-i.com/index.php?p=104)

Where as innerHTML is maybe more precise as you can have innerHTML, innerTEXT and more more things making it better practice then document.write.

Hope this makes things clearer,
Ian
0
VIkasumitCommented:
Hi,

Document.write Simple write the line on the page where it is called, where as body.innerHTML use the DOM object to determine the exact location of the Body Tag within HTML Document


Can you please let me know what is the subsequent function. and what is the error you are getting

VIkasumit
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
etd_onlineguysAuthor Commented:
Vikasumit,
I'm receiving an "object expected" error.  It's happening as the script tries to do some window size calculations in JS for a DHTML dropdown menu.  I didn't create the site; I'm making a simulation of it for training.  What I'm doing is putting the HTML to JS strings so that I can create specific pages with a few function calls.  I don't receive an error with the document.write(fnMyFunction()) method, but I do with the document.body.innerHTML = fnMyFunction().  I can get around the problem with document.write, so I'm not overly concerned with the error  My concern is that my understanding of these two methods is obviously flawed, since I believe they should behave identically.  
David
0
etd_onlineguysAuthor Commented:
I realize what was happening.  Part of the HTML I was trying to write using these methods were <script> includes.  These were not being included when I wrote them to the innerHTML of the body object, so I was getting errors when the script couldn't find the functions.  

Thanks for your help, guys.
0
Ian GoughCommented:
Hey n/p glad you got it sorted.
Ian
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.