469,632 Members | 1,698 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,632 developers. It's quick & easy.

Trouble using appendChild for document body in another frame

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);

Dec 19 '05 #1
3 6367


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
--

Martin Honnen
http://JavaScript.FAQTs.com/
Dec 19 '05 #2
Martin Honnen wrote:
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];


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
Dec 19 '05 #3
ezmiller wrote:
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?

Dec 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Stumped and Confused | last post: by
7 posts views Thread by Andrew Poulos | last post: by
10 posts views Thread by whisher | last post: by
1 post views Thread by Dmitry Kulinich | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.