Opera bug? -- disappearing <form> ?!

H

Howard Jess

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


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html><head><title>form</title><script type="text/javascript">
if (document.createElement) {
_obj = document.createElement('form');
alert('_obj: ' + _obj);
}
function olfunc() {
var f = document.getElementById('theForm');
var ftags = document.getElementsByTagName('form');
alert('f = ' + f + '\n' +
'# forms = ' + ftags.length);
}
window.onload = olfunc;
</script></head><body><div><form id="theForm" action=""><p>
text of a paragraph in theForm</p></form></div></body></html>


The alert boxes say:

_obj: [object HTMLFormElement]

and

f = null
# forms = 0


hj
 
R

Randy Webb

Howard said:
[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.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html><head><title>form</title><script type="text/javascript">
if (document.createElement) {
_obj = document.createElement('form');

Here, you create an object but you never append it to anything, so it
never gets created in the DOM of the page.
alert('_obj: ' + _obj);
}
function olfunc() {
var f = document.getElementById('theForm');
var ftags = document.getElementsByTagName('form');
alert('f = ' + f + '\n' +
'# forms = ' + ftags.length);
}
window.onload = olfunc;
</script></head><body><div><form id="theForm" action=""><p>
text of a paragraph in theForm</p></form></div></body></html>


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>

Changing your code to something like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>form</title>
<script type="text/javascript">
function olfunc() {
if (document.createElement) {
_obj = document.createElement('form');
document.body.appendChild(_obj);
//above, the obj is appended to the body
//and becomes part of the DOM Tree and can be
//found in the forms collection.
alert('_obj: ' + _obj);
}
var f = document.getElementById('theForm');
var ftags = document.getElementsByTagName('form');
alert('f = ' + f + '\n' + '# forms = ' + ftags.length);
}
window.onload = olfunc;
</script>
</head>
<body>
<div>
<form id="theForm" action="">
<p>text of a paragraph in theForm</p>
</form>
</div>
</body>
</html>

Where the call is made after the page has loaded, and it appends the obj
to the body of the page, all give 2 forms elements in IE6, FireFox and
Opera 7 (sorry, too lazy to download/install Opera 8). Perhaps you could
test the above code and see if it still displays the behavior you are
describing?
 
H

Howard Jess

Randy said:
Howard said:
[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 ...
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.length=0; the second div reports childNodes.length=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><title>form</title><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><script type="text/javascript">

if (document.createElement) {
var s = '';
obj = document.createElement('form');
s += 'obj: ' + obj + '\n';
if (window.location.search.substring(1) == 'more') {
div = document.createElement('div');
div.innerHTML = '<form name="form1"></form>';
s += 'div.childNodes: ' + div.childNodes.length +
'\nfirstChild: ' + div.firstChild + '\n';
div2 = document.createElement('div');
div2.innerHTML = '<form name="form2"></form>';
s += 'div2.childNodes: ' + div2.childNodes.length +
'\nfirstChild: ' + div2.firstChild + '\n';
}
alert(s);
}

function olfunc() {
var pre;
var f = document.getElementById('theForm');
var ftags = document.getElementsByTagName('form');
alert('f = ' + f + '\n' +
'# forms = ' + ftags.length);
}
window.onload = olfunc;
</script></head><body><div><form id="theForm" action=""><input type="hidden" name="h1"><p>
text of a paragraph in theForm</p></form><form action="" id="form2"><input
type="hidden" name="h2"></form></div></body></html>
 
H

Hallvord R. M. Steen

In Opera 8.01 (Linux; Build 1204) and in Opera 7.54 (Windows XP; Build
3865), my form disappears from the HTML markup (below).

It is a bug, and it's a very annoying one that is hard to work around.
The only suggestion I have is to use a timeout thread for the test - it
can be a very short timeout, still seems to fix it. Is that a workaround
that may work for you?

setTimeout('document.createElement("form");/* bug detection here */', 1);
 
H

Howard Jess

Hallvord said:
It is a bug, and it's a very annoying one that is hard to work around.
The only suggestion I have is to use a timeout thread for the test - it
can be a very short timeout, still seems to fix it. Is that a workaround
that may work for you?

setTimeout('document.createElement("form");/* bug detection here */', 1);

Thanks; it just might. I'm a little paranoid, though, that all we can see
is that it *seems* to fix it; without knowing what's really happening,
I'm worried that the bug may pop back up in some other combination of
script and HTML files to be loaded, or who-knows-what-else.

But it's definitely worth my time to do more testing with. Thanks!

hj
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top