By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,576 Members | 1,399 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,576 IT Pros & Developers. It's quick & easy.

checking 'document.body' availability (opera)

P: n/a
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

Aug 24 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a


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

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 24 '05 #2

P: n/a
http://wednus.com/test.htm

code:

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

Aug 24 '05 #3

P: n/a
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..

Aug 24 '05 #4

P: n/a


Sundew Shin wrote:
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?
--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 24 '05 #5

P: n/a
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

Aug 24 '05 #6

P: n/a
Martin Honnen <ma*******@yahoo.de> writes:
Sundew Shin wrote:
<html>
<head>
<script>

....
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Aug 24 '05 #7

P: n/a


Lasse Reichstein Nielsen wrote:
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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 24 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.