Jump to content

Document Write()


Cod-nes

Recommended Posts

document.write is something you shouldn't be using, but instead something likedocument.getElementById("whatever_id_name_you_chose_for_the_HTML_element_to_place_text").innerHTML=variabledocument.getElementsByTagName("HTML_element_you_chose")[0].innerHTML=variable // 0 is the starting element of the array of the selection of the HTML element you chosehttp://www.w3schools.com/HTMLDOM/dom_obj_document.aspYou would place whatever property or method in the list after documentFor example:document.lastModifiedProgramming languages may have different ways to join strings together (concatenation). From what I have worked with, it is either the + or .JavaScript uses the + operator for joining the strings.

Link to comment
Share on other sites

Thanks. :)
var LS=" ";document.write("text" . LS)

Why? Is something wrong with it?

As mma_fighter123 has mentioned it, JavaScript uses + to join strings. So, you could try
var LS=" ";document.write("text" + LS)

but try to use the innerHTML methods instead.

Link to comment
Share on other sites

Why? Is something wrong with it?
I think the most common reason people around here suggest that it be avoided is because it acts a little funny if you attempt to use document.write after the page has loaded. If you're just learning, or you're using it for debugging purposes, or you're writing to a popup window, there's nothing wrong with using it. Just be prepare to see a blank white page with plain text on it if you use it after the page loads.Here's an example that illustrates the "problem":
<html><head><script type="text/javascript">document.write("<h2>Hello from document.write!  This'll go away in a couple seconds.</h2>");window.onload = function(){	setTimeout("document.write('<h2>This comes after the page loads...</h2>');document.close();", 3000);}</script><style type="text/css">body {background-color:red;}</style></head><body></body></html>

Link to comment
Share on other sites

I started to round out this discussion by offering a code snippet where document.write() would be a good solution. I thought, what if you don't have server-side support, but you want to customize some features based on the season, time of day, etc. You might simply print the date at the bottom of the page. You might have an image of a reindeer if it's December.All these things could be handled with an inline script placed at the position of your document where you want the feature to appear. And in fact this is how we used document.write() many years ago.But for every example I came up with, I thought, No -- today I'd do that with innerHTML, a DOM method, dynamic CSS, or something else.Even for debugging, I prefer alert() because it doesn't alter the page's appearance, and it's safe after page-load.Used as part of a very limited tutorial, as is done at the W3Schools, I can see it, because it's efficient.But of course, the downside to the tutorials is the way they implicitly endorse document.write() as part of the normal javascript set of tools. Which plainly it isn't.

Link to comment
Share on other sites

I agree with Deirdre's Dad. The only use I really have ever had for document.write is when I need to generate the content for a popup window and I don't have any server-side solution set up. Fairly rare, but it happens from time to time. Here's an example:

<html><head><script type="text/javascript">function openWindow(){    var wnd = window.open("","somerandomnewwindow");    var html = "<html>";    html += "<body>";    html += "<h1>Hello World!</h1>";    html += "</body>";    html += "</html>";    wnd.document.open();    wnd.document.write(html);    wnd.document.close();}</script></head><body><button onclick="openWindow()">Open up a window!</button></body></html>

But of course, the downside to the tutorials is the way they implicitly endorse document.write() as part of the normal javascript set of tools. Which plainly it isn't.
It's not a part of javascript, but, then again, neither are window.open, [window.]alert, [window.]confirm, [window.]setTimeout, document.createElement, etc. etc. They're a part of the DOM, which javascript accesses. As far as I can tell, document.write is part of the DOM too:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-75233634Not core functionality, but a part of the HTML specification, none the less.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...