Click to See Complete Forum and Search --> : document.write and document.getElementById


Gollum
08-11-2003, 05:58 AM
Can anyone confirm whether or not this is a good idea...

I want to create HTML in the page using document.write and then immediately afterwards use document.getElementById to access one of the objects defined in it. e.g.
[code]
<html>
...
<script>
document.write('<span id="mySpan"></span>');
var oSpan = document.getElementById('mySpan');
// then use DOM to fill the span
</script>
...
</html>

I've seen a few javascripts doing this, however I've read that document.write may buffer the text and the only way to guarantee it's processed is to call document.close - which can't be done here

Charles
08-11-2003, 06:22 AM
Originally posted by Gollum
Can anyone confirm whether or not this is a good idea...Bad idea. You'll end up with a JavaScript dependent page.

Gollum
08-11-2003, 06:40 AM
Bad idea. You'll end up with a JavaScript dependent page.
Thanks Charles, but the site I'm working on is built to cope with Javascript enabled and disabled browsers separately. For the disabled browsers, they get plain html, and it leaves me free to use all the benefits of Javascript for those who do have it.

So is it still a bad idea?

Gollum
08-12-2003, 01:59 AM
No reli!
Are there browsers that won't let me do this?

Fang
08-12-2003, 04:52 AM
In Amaya you won't see any content ;)
It should work in most browsers, it's only when you write to a new window that not closing the document stream causes problems.