Michael Winter wrote:
[...]
When you call the write (or writeln) method, the argument is converted
to a string and written to the document stream. However, if the stream
is closed (which happens once the document has finished loading or the
document.close method is called), the stream is re-opened. This causes
the document, and everything in it (including your scripts and forms)
to be deleted.
Whilst that is what the spec says *should* happen, and it does as far
as I can tell in IE and Firefox (and likely Mozilla and Netscape),
scripts seem to linger in Safari and OmniWeb.
The point being that for some browsers, scripts are not replaced (or at
least, not entirely). I guess this is a bug or at best an
inconsistency with the HTML specification, but developers should not
expect that all contents are removed just by re-opening the document
and writing to it.
For example:
<html><head><title>play</title>
</head><body>
<script type="text/javascript">
function firstFunction(){
var buttonType='<input type="button" value="Proceed" name="contOn" '
+ ' onclick="secondFunction();">'
document.write(buttonType);
document.close();
}
function secondFunction() {
alert('I am the second function');
}
</script>
<p>Here is some text</p>
<button onclick="firstFunction();">firstFunction</button>
<p>Here is some more text</p>
</body</html>
When the first button is clicked, it writes a new button to the page
with an onclick that calls secondFunction. secondFunction should have
been deleted from the document, but it runs. Now this may be because
the script is held in memory (or for some other reason you may guess
at) but the point is it's still "there" in Safari.
Having said that, is there a more explicit way of emptying the
document contents? Perhaps by removing the HTML element?
--
Rob