views:

701

answers:

3

Consider two web pages with the following in their body respectively:

<body>
<script>
document.writeln('<textarea></textarea>')
</script>
</body>

and

<body>
<script>
var t = document.createElement('textarea');
document.body.appendChild(t);
</script>
</body>

(think of them as part of something larger, where the textareas have to be generated from JavaScript and can't be hard-coded into the page). They both produce the same output, but the former is considered "bad", while the latter is considered the "right" way to do it. (Right?)

On the other hand, if you type something in the page and then either refresh it, or go somewhere else and hit Back, then in the former case, what you typed in the textarea is preserved, while in the later it is lost. (At least on Firefox.)

Is there a way to use the latter method and still have the useful feature that what the user has typed into a form is saved even if they accidentally hit refresh or come back via the Back button (at least on Firefox)?

+4  A: 

I believe the document.write version actually blows away an existing content on the page. Ie, the body and script tags will no longer be there. That is why people usually use appendChild.

Keeping the text or not is very browser specific. I wouldn't bet that Firefox would not change it's behavior on that in a future version, either. I would suggest implementing an alert dialog when the user trys to navigate away from the page and there is unsaved content in edit fields. Usually you would do this with an unload event.

Chase Seibert
No, that's not true. document.write() will happily append to the existing content on the page.
ShreevatsaR
Actually, it will if you are not in the initial onload of the page. If you do it later, say when a Ajax call returns, it will erase the page.
Chase Seibert
Chase Seibert is correct that it will erase the page if it is called after the page has loaded.
some
Chase Seibert is also correct that the behavior is browser specific and I add that it also depends on the settings in the browser. Therefore you should not depend on this functionality.
some
this is about the document's open/closed/writing state and is a hangover of very early scripting tech - it has essentially been outmoded by DOM insertion and should be rarely used.
annakata
A: 

Would it be possible for you to add a hook which you could use to target the new textarea into? eg. a <div>

<div id="addtextarea"></div>

var e = document.getElementByID('addtextarea');
e.innerHTML = '<textarea></textarea>';

Then if you needed to remember the contents you could take either the innerHTML value to include the HTML or just the text node value of the textarea to extract the text.

sanchothefat
+2  A: 

document.write() won't blow away the page content as long as it is executed inline as the page is rendered. Online advertising makes extensive use of document.write() to dynamically write adverts into the page as it loads.

If however, you executed the document.write() method at a later time in the page history (after the body is completely rendered) then as Chase says, it would blow away the existing body and display the argument to document.write().

Other than that I agree that the preserving forms behaviour is really pretty browser specific, and not something you should rely on in many cases. It's a feature there to help the user rather than something for developers to be aware of or attempt to utilize.

Andy Hume