A problem with document.write in ff?

T

Tom

Hi, this snippet of code gives an error in Firefox but works just fine
in IE.

<html>
<head>
<script type="text/javascript">
function start(){
statusarray = new Array();
document.write("Test"); // This outputs text as expected
statusarray[1]="0"; //FF says that statusarray is undefined.
}
</script>
</head>
<body onload="start()">
</body>
</html>

The error only occurs if the document.write line is present. If I
remove that or comment it out, then it works just fine.

Don't worry about the utter lack of functionality, I have only drilled
down to the troublesome code for this example.

Tom...
 
L

Lee

Tom said:
Hi, this snippet of code gives an error in Firefox but works just fine
in IE.

<html>
<head>
<script type="text/javascript">
function start(){
statusarray = new Array();
document.write("Test"); // This outputs text as expected
statusarray[1]="0"; //FF says that statusarray is undefined.
}
</script>
</head>
<body onload="start()">
</body>
</html>

The error only occurs if the document.write line is present. If I
remove that or comment it out, then it works just fine.

This is normal behavior.
The first time you call document.write() after the page has been rendered, the
current contents of the document are cleared. That includes any variables and
functions.
The document.write() method is not generally useful for adding content to a
page. You probably want to modify the innerHTML attribute of some container.

<html>
<head>
<script type="text/javascript">
function start(){
statusarray = new Array();
document.getElementById("dynamic").innerHTML+="Test";
statusarray[1]="0";
}
</script>
</head>
<body onload="start()">
<div id="dynamic">
</body>
</html>
 
M

Martin Honnen

Tom wrote:

<html>
<head>
<script type="text/javascript">
function start(){
statusarray = new Array();
document.write("Test"); // This outputs text as expected
statusarray[1]="0"; //FF says that statusarray is undefined.
}
</script>
</head>
<body onload="start()">

When you call document.write after the document has been loaded as your
example does the browser does an implicit document.open and overwrites
the existing document, including variables. When old variables are gone
exactly depends on the browser and perhaps even the version of a
particular browser but if you really need document.write after the
document has been loaded then a safe way is to assemble all contents to
write first and then do one
document.open();
document.write(newContent);
document.close();
 
T

Tom

Thanks for that.

So I guess the fact that IE doesn't give the error is just IE doing
stuff badly again? :)

I only had the document.write in there as a debugging aid anyway, I
will use my debugmessage function (that modifies some innerHTML as
mentioned above) that I should have used anyway.. *sigh* lol.

cheers,

Tom...
 
R

RobG

Tom said:
Thanks for that.

So I guess the fact that IE doesn't give the error is just IE doing
stuff badly again? :)

I don't think you can say it's IE being bad. As Martin said, browsers
will clear the stuff in memory when they want - Firefox does it as
soon as document.open() is called, or at the end of a first
document.write if it wasn't. IE seems to keep it all in memory until
document.close().

It's just different behaviour.

[...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top