?
Solved

Difference between document.write and document.body.innerHTML

Posted on 2006-04-24
8
Medium Priority
?
2,035 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:etd_onlineguys
  • 4
  • 3
8 Comments
 
LVL 12

Expert Comment

by:Ian Gough
ID: 16528681
Hi!
This should claify things,
Ian
0
 
LVL 12

Expert Comment

by:Ian Gough
ID: 16528687
http://www.wait-till-i.com/index.php?p=104

Sorry with the link it's better!
Ian
0
 
LVL 2

Author Comment

by:etd_onlineguys
ID: 16528859
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 12

Assisted Solution

by:Ian Gough
Ian Gough earned 500 total points
ID: 16528950
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
 
LVL 4

Accepted Solution

by:
VIkasumit earned 500 total points
ID: 16532152
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
 
LVL 2

Author Comment

by:etd_onlineguys
ID: 16533414
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
 
LVL 2

Author Comment

by:etd_onlineguys
ID: 16536543
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
 
LVL 12

Expert Comment

by:Ian Gough
ID: 16537488
Hey n/p glad you got it sorted.
Ian
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question