Trouble using appendChild for document body in another frame

E

ezmiller

So I have some code that gets the body element of another frame and
then tries to
dynamically write a table. The code fails when, after creating the
table, I try to
append it to the document. I get an invalid argument error.

Here's the code....

var docBody =
top.frames[frameName].document.getElementsByTagName("BODY")[0];
alert(docBody.tagName);
var t;
var r;
var c;

// Dynamically write out table and header row.
t = document.createElement("table");
r = t.insertRow();
c = document.createElement("td");
c.appendChild(document.createTextNode("Student Name"));
r.appendChild(c);
c = document.createElement("td");
c.appendChild(document.createTextNode("Test#1"));
r.appendChild(c);
c = document.createElement("td");
c.appendChild(document.createTextNode("Test#2"));
r.appendChild(c);
c = document.createElement("td");
c.appendChild(document.createTextNode("Test#3"));
r.appendChild(c);
c = document.createElement("td");
c.appendChild(document.createTextNode("Test#4"));
r.appendChild(c);
docBody.appendChild(t);
 
M

Martin Honnen

ezmiller wrote:

var docBody =
top.frames[frameName].document.getElementsByTagName("BODY")[0];

For a start try with
var frameDoc = top.frames[frameName].document;
if (frameDoc) {
var docBody = frameDoc.getElementsByTagName("BODY")[0];
var t = frameDoc.createElement("table");
// now make sure you use frameDoc.createElement/createTextNode
// to create any elements or text nodes
// supposed to be inserted in that frame
 
T

Thomas 'PointedEars' Lahn

Martin said:
ezmiller said:
var docBody =
top.frames[frameName].document.getElementsByTagName("BODY")[0];

For a start try with
var frameDoc = top.frames[frameName].document;
if (frameDoc) {
var docBody = frameDoc.getElementsByTagName("BODY")[0];

HTMLDocument objects have a `body' property that refers to
a HTMLBodyElement object which in turn represents the
`body' element of the document. There is no need to use
HTMLDocument::getElementsByTagName() here.

<URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-26809268>


PointedEars
 
T

Tony

ezmiller said:
So I have some code that gets the body element of another frame and
then tries to
dynamically write a table. The code fails when, after creating the
table, I try to
append it to the document. I get an invalid argument error.

Have you tried a minimal test case? Start with ONLY one element, then
build up from there until it doesn't work.

I can't see offhand what's wrong with what you're doing, but I'll bet
that will help narrow it down.
var docBody =
top.frames[frameName].document.getElementsByTagName("BODY")[0];
alert(docBody.tagName);

Why use getElementsByTagName("BODY") - rather than just document.body ?
Maybe this is the problem?
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top