checking 'document.body' availability (opera)

S

Sundew Shin

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 '[object HTMLBodyElement]' or '[object]', so I could use the
'window.onload' event as the sign of 'document.body' element available.

but in Opera(8.01), it alerts 'null', and it tells the 'document.body'
element is not loaded yet.


after which 'window.' event, 'document.body' is become
available(ensured) for Opera?

thanks in advance
 
M

Martin Honnen

Sundew Shin wrote:

In Mozilla and IE, the function below:

window.onload = function(){
alert(document.body);
};

alerts '[object HTMLBodyElement]' or '[object]', so I could use the
'window.onload' event as the sign of 'document.body' element available.

but in Opera(8.01), it alerts 'null'

That sounds rather odd, could you post a URL where that happens?
 
S

Sundew Shin

a ha!

I have realized that there is no '<body></body>' tag in my test code.

so the fact is, Mozilla & IE generates 'document.body' element
automatically even when the scriptor forget to write it, but opera
doesn't.

I still don't understand why it implemented differently though..
 
M

Martin Honnen

Sundew said:
http://wednus.com/test.htm

code:

<html>
<head>
<script>
window.onload = function(){
alert(document.body);
};
</script>
</head>
</html>

That is an edge case, you realize that your HTML document does not
contain a <body> element. While other browsers automatically add a body
element to the DOM (and the HTML 4 SGML DTD supports that) I think it is
known that Opera does not do that. I don't know whether Opera intends to
fix that, you could ask in a group on news.opera.com.
But I am pretty sure that document.body is an element object in Opera's
DOM if your HTML document really contains a <body> element.
Why exactly do you need to use a source document without a <body> element?
 
S

Sundew Shin

thanks for your kind explanation, and I actually have no specific
reason to avoid <body>. it was just happend while I was making my
prog. to support opera. thanks.

Sundew Shin
Wednus Project
Developer/Manager
http://wednus.com
 
L

Lasse Reichstein Nielsen

Martin Honnen said:
Sundew Shin wrote:
....

While other browsers automatically add a
body element to the DOM (and the HTML 4 SGML DTD supports that) I
think it is known that Opera does not do that.

Perfectly reasonable, since there is no indication that the page is
HTML 4 (and quite a lot that it isn't, like missing DOCTYPE declaration,
title element, type attribute on script element and ... missing body
element :)

/L
 
M

Martin Honnen

Lasse said:
Perfectly reasonable, since there is no indication that the page is
HTML 4 (and quite a lot that it isn't, like missing DOCTYPE declaration,
title element, type attribute on script element and ... missing body
element :)

Opera has troubles creating a head element for instance in this valid
HTML 4 document:
<http://home.arcor.de/martin.honnen/operaBugs/op8/headMissingInDOM1.html>
So even when the HTML 4 DTDs suggest that the parser needs to add an
element like the <head> element Opera does not do that.

But you are right that the original test case needs improvements like a
body element or some body element content added to make it valid HTML
and then to be able to decide whether the DOM should have an element
object or not.
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top