469,950 Members | 2,035 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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

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..
Jul 23 '05 #1
4 1682

Zhang Weiwu wrote:
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.


Post a URL to a test case which exhibits the problem. Run the document
through the validator http://validator.w3.org/ first.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
Zhang Weiwu wrote:
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>
Jul 23 '05 #3
Zhang Weiwu wrote:
Zhang Weiwu wrote:
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.

--
Rob
Jul 23 '05 #4

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
yo**@mandala.com
<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>

Zhang Weiwu wrote:
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..


Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Ron | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.