473,546 Members | 2,289 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

document.body & appendChild question

Hi everyone.

I'm attempting to write a Javascript that will create a form within a
brand-new document in a specific frame of a frameset. The problem is
that I can create the form and input element using createElement() , but
when I go to append the form element into the new document, the script
halts and I get the following error in my Javascript Console (Firefox 1.0):

__tmp_newDoc.bo dy has no properties.

Here is the frameset definition:

Code:
<frameset rows="30%,30%,* " border="1" noresize>
<frame src="" name="FRAME01" border="1"/>
<frame src="" name="FRAME02" border="1"/>
<frame src="framedom02 .html" name="FRAME03" border="1"/>
</frameset>
framedom02.html essentially just loads the Javascript library and calls
a function from onLoad in the BODY tag.

The javascript being used is as follows:

Code:

function createForm()
{
_TMP_FORMNAME = "form01";

// Open a new document for FRAME02.
__tmp_newDoc = parent.frames['FRAME02'].document.open( );

__tmp_htmlObj = __tmp_newDoc.cr eateElement("ht ml");

alert("Created HTML Object");

__tmp_bodyObj = __tmp_newDoc.cr eateElement("bo dy");

alert("Created BODY Object");

// Create a Form Object.
__tmp_formObj = __tmp_newDoc.cr eateElement("fo rm");
__tmp_formObj.n ame = _TMP_FORMNAME;
__tmp_formObj.a ction = "framehandler02 .php";
__tmp_formObj.m ethod = "post";
__tmp_formObj.t arget = "FRAME01";

alert("Created FORM Object");

// Create an Input Element object.
__tmp_input01 = __tmp_newDoc.cr eateElement("in put");
__tmp_input01.t ype = "text";
__tmp_input01.v alue = "BLAH";
__tmp_input01.n ame = "FRMTXT01";

alert("Created INPUT Object");

// Append the Input element object to the Form Object.
__tmp_formObj.a ppendChild(__tm p_input01);

alert("Appended INPUT to FORM");

__tmp_bodyObj.a ppendChild(__tm p_formObj);

alert("Appended FORM to BODY");

__tmp_htmlObj.a ppendChild(__tm p_bodyObj);

alert("Appended BODY to HTML");

// Append the Form Object to the document body.
__tmp_newDoc.ap pendChild(__tmp _htmlObj);

alert("Appended HTML to Document");

// Close the new document in FRAME02
__tmp_newDoc.cl ose();
}
.... it NEVER makes it to the last Alert. I've tried a few other ways of
doing it, but haven't had any results with them either. What am I doing
wrong here?

Thanks,

- skubik.
Jul 23 '05 #1
6 3679


skubik wrote:

<frameset rows="30%,30%,* " border="1" noresize>
<frame src="" name="FRAME01" border="1"/>
Try with
<frame src="about:blan k"
__tmp_newDoc = parent.frames['FRAME02'].document.open( );


Don't use the document.open call, it is needed if you want to use
document.write on the document but not for DOM manipulation.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
skubik wrote:
<snip>
... I get the following error in my Javascript Console
(Firefox 1.0):

__tmp_newDoc.bo dy has no properties.
Given that the posted code does not include the property accessor -
__tmp_newDoc.bo dy - it is unlikely that your problem is entirely
contained in the posted function. If this is the only error reported
then fixing the problems in that function will not correct that error.
An if it is not the only error, and not the first error, then it would
be advisable to address the generated errors in the order in which they
happen.

<snip> function createForm()
{
_TMP_FORMNAME = "form01";

// Open a new document for FRAME02.
__tmp_newDoc = parent.frames['FRAME02'].document.open( );
The - open - method of documents does not have a return value so the
above line will assign an undefined value to - __tmp_newDoc -.
__tmp_htmlObj = __tmp_newDoc.cr eateElement("ht ml");
Given that - __tmp_newDoc - has an undefined value, this line should
error ("__tmp_newD oc has no properties"). No more code form this
function will execute after the error.
__tmp_bodyObj = __tmp_newDoc.cr eateElement("bo dy"); <snip> __tmp_formObj = __tmp_newDoc.cr eateElement("fo rm"); <snip> __tmp_input01 = __tmp_newDoc.cr eateElement("in put");
I assume that you are using all of these strange variable names in order
to avoid naming conflicts in the global namespace. However, there seems
little chance that all (if any) of these variables should be exposed in
the global namespace.

Wherever possible function local variables should be used, which avoids
any concerns about naming conflicts in the global namespace. It also
generally results in better (more direct, comprehensible, maintainable,
manageable, ect.) code.

<snip> __tmp_newDoc.cl ose();


The - document.open - and - document.close - methods are intended for
use with - document.write/ln - and are not appropreate in code that is
using DOM Core node creation/manipulation methods (and are very likely
to cause all sorts of cross-browser issues in this context).

Richard.
Jul 23 '05 #3
skubik wrote:
Hi everyone.

[...]

Combining the advice above:

<frame src="about:blan k" name="FRAME02" border="1"/>

and your script should look like (all those underscores and
capitals are ugly IMHO, but use 'em if you like):

function createForm(){
var formName = 'form01';
var F = parent.frames['FRAME02'];
var docBody = F.document.getE lementsByTagNam e('body')[0];

// Create a Form Object.
var formObj = document.create Element("form") ;
formObj.name = formName;
formObj.action = "framehandler02 .php";
formObj.method = "post";
formObj.target = "FRAME01";

// Create an Input Element object.
var input01 = document.create Element("input" );
input01.type = "text";
input01.value = "BLAH";
input01.name = "FRMTXT01";

// Append the Input element object to the Form Object.
formObj.appendC hild(input01);
docBody.appendC hild(formObj);
}
Incidentally, best to declare vairables with "var" to keep 'em
local. That way they are cleaned up when the function ends and
don't linger as globals.

--
Rob
Jul 23 '05 #4
Thanks RobG (and others) for your help. I rewrote the function and it
works great now. But I'm having a problem submitting the form from
Javascript now.

Here's the code that I'm using to submit the form (sorry about the
'strange' variable names, I'm just strange I guess):

const FORMNAME = "form01";

function sendForm()
{
var __tmp_formObj = parent.frames['FRAME02'].document.forms[FORMNAME];

__tmp_formObj.e lements[0].value = "CHANGED!";

alert("Action: " + __tmp_formObj.a ction + "\nMethod: " +
__tmp_formObj.m ethod + "\nTarget: " + __tmp_formObj.t arget);

__tmp_formObj.s ubmit();
}

This function is called by clicking on a hyperlink in the same frame
that the script itself resides in (in this case, FRAME03).

Everything works up until the submit function. Here is what Firefox's
Javascript Console came up with:

Error: uncaught exception: [Exception... "Component returned failure
code: 0x804b000a [nsIDOMHTMLFormE lement.submit]" nsresult: "0x804b000a
(<unknown>)" location: "JS frame ::
http://localhost:8000/homepage/dev/jsdom02.js :: sendForm :: line 41"
data: no]

Everything displayed in the alert comes up properly, so I assume that I
'have' the right object, so why is it that .submit() won't work?

TIA,

- skubik.
RobG wrote:
skubik wrote:
Hi everyone.


[...]

Combining the advice above:

<frame src="about:blan k" name="FRAME02" border="1"/>

and your script should look like (all those underscores and
capitals are ugly IMHO, but use 'em if you like):

function createForm(){
var formName = 'form01';
var F = parent.frames['FRAME02'];
var docBody = F.document.getE lementsByTagNam e('body')[0];

// Create a Form Object.
var formObj = document.create Element("form") ;
formObj.name = formName;
formObj.action = "framehandler02 .php";
formObj.method = "post";
formObj.target = "FRAME01";

// Create an Input Element object.
var input01 = document.create Element("input" );
input01.type = "text";
input01.value = "BLAH";
input01.name = "FRMTXT01";

// Append the Input element object to the Form Object.
formObj.appendC hild(input01);
docBody.appendC hild(formObj);
}
Incidentally, best to declare vairables with "var" to keep 'em
local. That way they are cleaned up when the function ends and
don't linger as globals.

Jul 23 '05 #5


skubik wrote:

const FORMNAME = "form01";
Just a note, const is a Mozilla/Netscape JavaScript extension not
implemented by other engines, if you are writing for an intranet where
only Mozilla browsers are used then it is fine to use const but if you
intend to write for the web then don't use const, use var.
function sendForm()
{
var __tmp_formObj = parent.frames['FRAME02'].document.forms[FORMNAME];

__tmp_formObj.e lements[0].value = "CHANGED!";

alert("Action: " + __tmp_formObj.a ction + "\nMethod: " +
__tmp_formObj.m ethod + "\nTarget: " + __tmp_formObj.t arget);

__tmp_formObj.s ubmit();
}

This function is called by clicking on a hyperlink in the same frame
that the script itself resides in (in this case, FRAME03).

Everything works up until the submit function. Here is what Firefox's
Javascript Console came up with:

Error: uncaught exception: [Exception... "Component returned failure
code: 0x804b000a [nsIDOMHTMLFormE lement.submit]" nsresult: "0x804b000a
(<unknown>)" location: "JS frame ::
http://localhost:8000/homepage/dev/jsdom02.js :: sendForm :: line 41"
data: no]

Everything displayed in the alert comes up properly, so I assume that I
'have' the right object, so why is it that .submit() won't work?


Hard to tell, can you try to make a test case as small as possible to
reproduce that problem and then post the URL here?

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #6
Martin Honnen wrote:
Hard to tell, can you try to make a test case as small as possible to
reproduce that problem and then post the URL here?


No prob. Here's the URL:

http://www.gotkube.com/dev/frame02.html

Click the 'Send Form' link in the red frame to start the function in
question.

Any thoughts as to what's going on? Turns out that my PHP script that
handles the form once it's submitted wasn't in the same directory, but
I'm still not getting it to even SEND the form with .submit().

- skubik.
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2767
by: Nicolas Van Lancker | last post by:
Hi folks; I have this webpage, allowing users to insert multiple records in one post into the database. Because I don't know the exact number of records they want to add, I created a little javascript that adds a new line with HTML inputboxes and lists to the form if they press a button.
2
9076
by: Brian | last post by:
Hi all. I have a bunch of pages that reference an external script from the head section. I'd like to add additional <script> elements to the pages' bodies, but I can't edit the pages themselves. I'd like to add them from the existing script, but I need to make sure that the elements get added to the end of the document's body section. ...
7
2332
by: Sundew Shin | last post by:
Hi, I'm wanting to check the availability of the 'document.body' element. In Mozilla and IE, the function below: window.onload = function(){ alert(document.body); }; alerts '' or '', so I could use the
2
1793
by: X t l a n | last post by:
Hello. I've noticed that Mozilla browsers older than 1.1 release don't support document.body.clientWidth but I would like to know dimensions of client workspace area. How to get them? window.innerWidth returns information, which includes width of scrollbar (this is not useful space). I would like to subtract that value, but how to cope...
14
4049
by: christopher.secord | last post by:
Can someone please tell me why this works: document.body.innerHTML = "<table><tr><th>one</th></tr><tr><td>two</td></tr></table>"; But this does not work: document.body.innerHTML = "<table><tr>"; document.body.innerHTML = document.body.innerHTML + "<th>one</th></tr><tr><td>two</td></tr></table>";
2
4425
by: Earl Teigrob | last post by:
I am trying to build a custom control to wrap my smart navigation implimention (not microsofts 'cause it has problems) The follow code works fine when the onclick and onload events are defined in the Body tag. However, when I try to set them in Javascript code, I get errors. What am I doing wrong??? **********THIS WORKS ************...
3
6525
by: ezmiller | last post by:
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.document.getElementsByTagName("BODY");
2
14638
by: =?Utf-8?B?SGFyZHkgV2FuZw==?= | last post by:
Hi all, I created a complex ASPX page, nested inside a master page and Ajax UpdatePanel. In one of my Javascript function I need to capture mouse position. I use alert(document.body.scrollTop) to test scroll bar position. Even though I have a long scroll bar and already to end of the page, when I call this function, I always get "0" as...
3
15585
by: jnag | last post by:
I am trying to implement font changer, where I have links on the banner of the site and when the user clicks on the links, the font size of the page has to change. I tried document.body.style.fontSize='50%' for example, on an onclick() event, but it did not work. I tried zooming with the following code, and it works: <td...
0
7435
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7694
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6026
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5360
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1921
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.