extremely strange behavor, not able to retrive an form through getElementById

Z

Zhang Weiwu

This is really very stange. I have a form like this:

<form id="form_A"> xxxx </form>

This form is on one page in a page of a web application, it is the only
form on that page. on the end of the page in javascript:
document.getElementById("form_A").submit();

In the begining it works very well, after I restarted the browser and
did some small modification (I don't recall, but far from chaning that
form or script), it doesn't work. After some debuging it shows
document.getElementById("form_A") gets nothing, element with this ID
does not exist.

I tried so many debugging:
1) if change <form id="form_A"> to <div id="form_A"> then
document.getElementById("form_A") works
2) if move <form id="form_A"> to just below <body> then
document.getElementById("form_A") works
3) if move <form id="form_A"> to just above </body> then
document.getElementById("form_A") return nothing
4) if move <form id="form_A"> to random place in the webpage
document.getElementById("form_A") return nothing
5) if remove all other javascript on that page, only have the HTML and
one line of javascript document.getElementById("form_A"), except if we
put the form just below <body>, otherwise
document.getElementById("form_A") return nothing
6) use HTMLTIDY it shows only warning but no error

all above are tested on mozilla 1.7.8, I picked up random cases to test
on Opera 8.0 Final, same behavor as on moz.

in all above case, if document.getElementById("form_A") return nothing,
other means to retrive the <form> element all failed. Including
* addresssing the form by "name" attribute;
* getElementsByTagName('form');
* alert(fatherelement.innerHTML).
It just vanishes perfectly! E.g if I display html.innerHTML then in the
dialogue box the whole HTML source is displayed, PERFECTLY without the
<form>

Any idea about why? I suppose javascript might be the
most-difficult-to-debug language on the earth. To really dig out the
reason of this strange behavor I must put <form> right above </body> and
remove other elements in the HTML source, once a half, and find out when
this behavor change. Such debuging would consume me A LOT of time.

I do program in many other languages, and I'd like to say javascript is
one of the most difficult to manage..
 
Z

Zhang Weiwu

Zhang said:
This is really very stange. I have a form like this:

<form id="form_A"> xxxx </form>

This form is on one page in a page of a web application, it is the only
form on that page. on the end of the page in javascript:
document.getElementById("form_A").submit();

In the begining it works very well, after I restarted the browser and
did some small modification (I don't recall, but far from chaning that
form or script), it doesn't work. After some debuging it shows
document.getElementById("form_A") gets nothing, element with this ID
does not exist.

I tried so many debugging:
1) if change <form id="form_A"> to <div id="form_A"> then
document.getElementById("form_A") works
2) if move <form id="form_A"> to just below <body> then
document.getElementById("form_A") works
3) if move <form id="form_A"> to just above </body> then
document.getElementById("form_A") return nothing
4) if move <form id="form_A"> to random place in the webpage
document.getElementById("form_A") return nothing
5) if remove all other javascript on that page, only have the HTML and
one line of javascript document.getElementById("form_A"), except if we
put the form just below <body>, otherwise
document.getElementById("form_A") return nothing
6) use HTMLTIDY it shows only warning but no error

all above are tested on mozilla 1.7.8, I picked up random cases to test
on Opera 8.0 Final, same behavor as on moz.

in all above case, if document.getElementById("form_A") return nothing,
other means to retrive the <form> element all failed. Including
* addresssing the form by "name" attribute;
* getElementsByTagName('form');
* alert(fatherelement.innerHTML).
It just vanishes perfectly! E.g if I display html.innerHTML then in the
dialogue box the whole HTML source is displayed, PERFECTLY without the
<form>

Any idea about why? I suppose javascript might be the
most-difficult-to-debug language on the earth. To really dig out the
reason of this strange behavor I must put <form> right above </body> and
remove other elements in the HTML source, once a half, and find out when
this behavor change. Such debuging would consume me A LOT of time.

I do program in many other languages, and I'd like to say javascript is
one of the most difficult to manage..

I am going to faint!!

I wish to save this webpage to local computer to research carefully.
What happens? I watched source viewer and made sure this <form> is in
source. click 'save as', and save a local copy, then this <form>
vanishes in the local copy!

By the way, this is a very normal form, normal as it could be:
<form id="launch_help"
action="/Byron/help/"
method="post" target="help_content">
<input name="help_topic" type="hidden" value="bookmarks"/>
</form>
 
R

RobG

Zhang said:
Zhang said:
This is really very stange. I have a form like this:

<form id="form_A"> xxxx </form>
[...]
In the begining it works very well, after I restarted the browser and
did some small modification (I don't recall, but far from chaning that
form or script), it doesn't work. After some debuging it shows
document.getElementById("form_A") gets nothing, element with this ID
does not exist.
[...]

The symptoms indicate that the browser has not finished creating your
form when you call getElementById so the browser returns an error. Run
your script from <body onload="..."> and you may have more luck.

A debug step is to put an alert in your script just before you call
getElementById - something like:

alert('about to call getElementById...');

The pause should give your browser time to finish the form before the
call is made.
 
J

Jeff

Use the MS Script Editor included free with MS Office 2002 and above,
for debugging Internet Explorer (IE).

This subject is of great interest to many JS developers, as there is no
obvious, low cost way to do sophisticated debugging in
IE6 other than to use the debugger described below, which is horribly
documented otherwise. I feel debugging is an important aspect of
projecting the useability of the language and needs to be made more
clear for new users.


Jeff Papineau
(e-mail address removed)


<FAQENTRY>

This is a page that describes how to install and use the MS Script
Editor to debug Javascript in Internet Explorer ( IE ). It has a
powerful debugger built into it that works really well for developers
supporting IE5+. This debugger/editor included with most versions of
Microsoft Office.

http://www.mandala.com/javascript/debug_javascript.html

..NET programmers may have better tools (VStudio) but this comes in
really handy for anyone developing with JSP and PHP and other dynamic
scripting languages which embed javascript, as well as any HTML page
using Javascript in Internet Explorer that needs a (almost) free
debugging environment.

</FAQENTRY>
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top