Randy Webb wrote:
Howard Jess wrote:
[Submitted as a bug to Opera; posted here in hopes of finding an explanation?]
In Opera 8.01 (Linux; Build 1204) and in Opera 7.54 (Windows XP; Build 3865),
my form disappears from the HTML markup (below). To summarize:
1) In a <script> block in the <head> I create a form element (part of
object/feature/bug detection).
2) There's a <form> element defined in the <body>, with the id 'theForm'.
3) The onload function tries to access that form, and also counts the
total number of forms in the document. It fails to get a reference
to the form; the count is 0.
Black magic?
No, less subtle than that. And its not a bug.
Yes, it is; I think you've missed my point ...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html><head><ti tle>form</title><script type="text/javascript">
if (document.creat eElement) {
_obj = document.create Element('form') ;
Here, you create an object but you never append it to anything, so it
never gets created in the DOM of the page.
That's correct (almost; it's not that I don't append anything to the
form object, but that the form object is not appended/inserted into the
document). But it's irrelevant. I *don't want* this object in the page;
I'm creating it to test the browser for an unrelated problem.
The alert boxes say:
_obj: [object HTMLFormElement]
and
f = null
# forms = 0
forms=0 might be an implementation idea since the form is empty <shrug>
No; there actually is no <form> element in the DOM tree.
Let me restate the bug (yes, I'm pretty sure this should be called a bug):
If you create a <form> element in a script during <head> processing,
then the first <form> element encountered during HTML parsing is lost.
Subsequent <form>s are handled as expected.
The following file illustrates this. It creates a form in the head, and
discards it. The HTML markup contains 2 forms; the onload function only
reports 1. (I've added content to each, so they're not empty.)
If you access this file with the query '?more', then two <div> elements
are also created during <head> processing; each is given a <form> using
its innerHTML (yes, non-standard, but bear with me). The first div
reports childNodes.leng th=0; the second div reports childNodes.leng th=1.
This seems to imply that the existence of a form, even though it's not
part of the document, messes up the parser when it first encounters a
form in markup.
Perhaps the parser it thinks it's already got an open <form>, and so
ignores any more until it sees </form>; but the open <form> is *not* one
that is part of the document, so it disappears ???
Sorry for the length of this, and what follows; I hope someone (from
Opera?) can confirm this?
hj
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html><head><ti tle>form</title><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><script type="text/javascript">
if (document.creat eElement) {
var s = '';
obj = document.create Element('form') ;
s += 'obj: ' + obj + '\n';
if (window.locatio n.search.substr ing(1) == 'more') {
div = document.create Element('div');
div.innerHTML = '<form name="form1"></form>';
s += 'div.childNodes : ' + div.childNodes. length +
'\nfirstChild: ' + div.firstChild + '\n';
div2 = document.create Element('div');
div2.innerHTML = '<form name="form2"></form>';
s += 'div2.childNode s: ' + div2.childNodes .length +
'\nfirstChild: ' + div2.firstChild + '\n';
}
alert(s);
}
function olfunc() {
var pre;
var f = document.getEle mentById('theFo rm');
var ftags = document.getEle mentsByTagName( 'form');
alert('f = ' + f + '\n' +
'# forms = ' + ftags.length);
}
window.onload = olfunc;
</script></head><body><div ><form id="theForm" action=""><inpu t type="hidden" name="h1"><p>
text of a paragraph in theForm</p></form><form action="" id="form2"><inp ut
type="hidden" name="h2"></form></div></body></html>