PDA

View Full Version : Very simple question regarding "document.write"


eyalros
04-10-2007, 05:47 AM
Hi,
Every time I use "document.write", it's clears all the stuff in the page.
how can I use "document.wirte" to write something in a new row without clearing all the stuff in my page?

thanks

wkkor
04-10-2007, 05:56 AM
It only can use while you are still processing for the page, and not allow to append to the page if some event process. The page will be clearing if you do so. You may try to use innerHTML or outerHTML instead of document.write. Hope it may helpful.

eyalros
04-10-2007, 06:32 AM
How can I use outerHTML (or similar thing) in Firefox?

wkkor
04-10-2007, 06:45 AM
Firefox cann't support outerHTML well except you write another script to fix it. Kindly to advise use innerHTML. :)

RysChwith
04-10-2007, 08:17 AM
You may have better luck using DOM methods (which is the standards-compliant way to accomplish this):

http://developer.apple.com/internet/webcontent/dom2i.html

Rys

coothead
04-10-2007, 08:20 AM
Hi there eyalros,

do as Rys suggested. :agree:

But if you find that initially rather daunting then start like this...

you should use document.getElementById instead of the deprecated document.write

Here is a simple example...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css">
#mydiv {
text-align:center;
}
</style>

<script type="text/javascript">
window.onload=function() {
writeSomething();
}
function writeSomething() {
document.getElementById('mydiv').firstChild.nodeValue=
'this is a replacement for the deprecated document.write()';
}
</script>

</head>
<body>

<div id="mydiv">nbsp;</div>

</body>
</html>