Mozilla, written html and getElementById

  • Thread starter Richard A. DeVenezia
  • Start date
R

Richard A. DeVenezia

Mozilla (1.4 win2k) doesn't like this

document.write ('<TABLE ID="abc"><TR><TD></TD></TR></TABLE>')
table = document.getElementById ('abc')
alert (table.rows.length)

I see in JS Console, Error: table has no properties

The same message that would appear if I tried to get real non-existant id
table = document.getElementById ('abc-doesnotexist')
alert (table.rows.length)

Any work arounds ( Not in a position to place my own null table in source
HTML:( ) ?
Should I skip making 1.4 happy and wait for Moz 1.5+ ?
 
R

Richard A. DeVenezia

Richard A. DeVenezia said:
Mozilla (1.4 win2k) doesn't like this

document.write ('<TABLE ID="abc"><TR><TD></TD></TR></TABLE>')
table = document.getElementById ('abc')
alert (table.rows.length)

I see in JS Console, Error: table has no properties


This happens in a js that is being loaded in the <HEAD> tag
 
R

Richard Cornford

Richard A. DeVenezia said:
This happens in a js that is being loaded in the <HEAD> tag

A table element within the HEAD of a document would be invalid so if the
function is called within the head it is unrealistic to expect it to
work. But I would be expect timing problems with this code anyway as
the browser may not get a chance to fully set up the objects created
with the document.write statement before it is references. (Indeed I
recall that at least one browser delays document.write output until the
completion of the current script block)

It would probably be safer to use DOM node insertion methods to create
and insert a valid table structure. Then you would have a reference to
it prior to inserting it and would not have to use getElementById at all
(or give the table an ID). Alternatively you would probably have to
split the code with the document.write inserting the HTML inline and
another "init" function being called onload (or with a timeout).

Richard.
 
L

Lasse Reichstein Nielsen

My guess:
You use document.write to insert context in the input stream after
the current script element. It is not parsed until after the script
tag ends.

Try ending the script tag after the document.write and start a new
script tag with the rest of the code.
This happens in a js that is being loaded in the <HEAD> tag

So, you are writing tables into the head tag? That means that
the head tag must have stopped, so the browser automatically ends
the head tag and starts the body tag.

/L
 
G

Gavino Paolini

Il Wed, 10 Sep 2003 11:32:52 +0200, Lasse Reichstein Nielsen ha scritto:
....
So, you are writing tables into the head tag? That means that the head
tag must have stopped, so the browser automatically ends the head tag
and starts the body tag.

/L

Maybe he meant that the script tag containing that portion of code is
inside the head and not in the body, I'm just supposing.

gp
 

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,085
Messages
2,570,597
Members
47,219
Latest member
Geraldine7

Latest Threads

Top