Click to See Complete Forum and Search --> : settimeout() and document.write woes


v1c10u5
02-27-2004, 08:44 PM
I'm using this code:

setTimeout('document.write("Bob missed John (0 damage - 50 left)<br><br>")',800);

Its meant to write into the current window, where I call the script, not on a new page

fredmv
02-27-2004, 08:53 PM
That's because when you call the document.write method after the page is done loading, it then calls the document.open and document.clear methods thus overwriting the entire document. What you need to do is use the functionality of the DOM to create new nodes in the DOM tree. That would be the preferred method, however, you could also consider using the non-standard innerHTML property, or for pure textual content, you could use firstChild.nodeValue or firstChild.data.

v1c10u5
02-28-2004, 05:13 AM
hmmm, i'm a bit of a newb here, so your gonna need to explain it a bit more

freefall
02-28-2004, 08:41 AM
In a nutshell, you can't write to a page after it is loaded.

There's a really simple way around that, though. Simply use a <div> element where you want the content to go. You can render HTML content in a div at any time and it looks just like its getting written to the page. Try this in the page

<div id="content"></div>

Then to write to the div, don't use document.write(). Instead, you have to get the div element, and edit its innerHTML. To access the innerHTML, use the following:

document.getElementById("content").innerHTML = "the code"

You can change the content of a div at any time, they're very handy!

See the design-in-progress of my site for an example:
www.degreethree.com/newsite
When you mouseover the links, the links which appear to the left are written into a div based on what the link is. I just use the div over and over.

Play around with it, see if this helps!
- Ian