checking 'document.body' availability (opera)

Discussion in 'Javascript' started by Sundew Shin, Aug 24, 2005.

  1. Sundew Shin

    Sundew Shin Guest

    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
     
    Sundew Shin, Aug 24, 2005
    #1
    1. Advertising

  2. 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/
     
    Martin Honnen, Aug 24, 2005
    #2
    1. Advertising

  3. Sundew Shin

    Sundew Shin Guest

    http://wednus.com/test.htm

    code:

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


    thanks.
     
    Sundew Shin, Aug 24, 2005
    #3
  4. Sundew Shin

    Sundew Shin Guest

    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..
     
    Sundew Shin, Aug 24, 2005
    #4
  5. 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/
     
    Martin Honnen, Aug 24, 2005
    #5
  6. Sundew Shin

    Sundew Shin Guest

    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
     
    Sundew Shin, Aug 24, 2005
    #6
  7. Martin Honnen <> 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 -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Aug 24, 2005
    #7
  8. 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/
     
    Martin Honnen, Aug 24, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Flavio codeco coelho

    Checking for X availability

    Flavio codeco coelho, Jan 11, 2005, in forum: Python
    Replies:
    4
    Views:
    934
    Cameron Laird
    Jan 16, 2005
  2. =?Utf-8?B?QVZM?=

    web service availability checking

    =?Utf-8?B?QVZM?=, Jul 19, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    499
    nahid
    Jul 20, 2007
  3. NightCrawler
    Replies:
    0
    Views:
    333
    NightCrawler
    Feb 12, 2008
  4. NightCrawler
    Replies:
    2
    Views:
    346
    Mark Space
    Feb 14, 2008
  5. Martin Rinehart

    document or document.body

    Martin Rinehart, Dec 6, 2008, in forum: Javascript
    Replies:
    13
    Views:
    185
    Martin Honnen
    Dec 7, 2008
Loading...

Share This Page