Connecting Tech Pros Worldwide Help | Site Map

No Body Node Upon Page Load?

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 8th, 2005, 06:15 AM
Patient Guy
Guest
 
Posts: n/a
Default No Body Node Upon Page Load?


I have this in a document:

<body onload="a_function(this);">

In an (external) script file, I define a_function() as follows:


function a_function(obj)
{
// surprise errant coding here
var docImages = obj.parentNode.getElementsByTagName("img");
// more code follows
}

When I change the 'body' tag to the following, it works:

<body onload="a_function(document.body);">

Much to my surprise, the 'obj' parameter is NOT the 'body' node of the
document (i.e., document.body). The object 'obj' is in fact an instance
of a 'window' class (the window object). This behavior is true in Firefox
(as seen in Venkman) and in MS Internet Explorer (as guessed by an error
in script).



Two and a half questions:

1. Why is 'obj' the window object and not document.body object (node)?

2. Is it proper to use lowercase arguments in the getElementsByTagName()
method? Suggested failsafe workarounds?



  #2  
Old November 8th, 2005, 10:42 AM
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
Default Re: No Body Node Upon Page Load?

Patient Guy wrote:
[color=blue]
> I have this in a document:
> <body onload="a_function(this);">
>
> In an (external) script file, I define a_function() as follows:
>
>
> function a_function(obj)
> {
> // surprise errant coding here
> var docImages = obj.parentNode.getElementsByTagName("img");
> // more code follows
> }
>
> [...]
> Much to my surprise, the 'obj' parameter is NOT the 'body' node of the
> document (i.e., document.body). The object 'obj' is in fact an instance
> of a 'window' class (the window object). This behavior is true in Firefox
> (as seen in Venkman) and in MS Internet Explorer (as guessed by an error
> in script).[/color]

Confirmed for Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12)
Gecko/20050922 Firefox/1.0.7 (Debian package 1.0.7-1) Mnenhy/0.7.2.0.

Also

<body onload="alert(this)">

for that user agent, meaning it is not a matter of a user-defined (external)
function.
[color=blue]
> Two and a half questions:
>
> 1. Why is 'obj' the window object and not document.body object (node)?[/color]

Seems to be a side effect of DOM Level 0, where there is an `onload'
property of the Global Object (read: the Window object) accepting a
function reference to be called when the document was loaded. So that

<body onload="foo(this)">

would result in

window.onload = function()
{
foo(this);
}

where `this' would refer to the Window object, instead of the expected

document.body.onload = function()
{
foo(this);
}

(proprietary) or

document.body.addEventListener("load", new Function("foo(this);"), false);

(standards-compliant) where `this' would refer to the HTMLBodyElement
object.

You could file a bug in Bugzilla if there is not already one.
[color=blue]
> 2. Is it proper to use lowercase arguments in the getElementsByTagName()
> method?[/color]

Yes, as long as a non-XML document type is concerned.

<http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-A6C9094>
[color=blue]
> Suggested failsafe workarounds?[/color]

All lowercase and uppercase combinations of the element type.


PointedEars
  #3  
Old November 8th, 2005, 10:42 AM
Julian Turner
Guest
 
Posts: n/a
Default Re: No Body Node Upon Page Load?


Patient Guy wrote:
[color=blue]
> I have this in a document:
>
> <body onload="a_function(this);">
>
> In an (external) script file, I define a_function() as follows:
>
>
> function a_function(obj)
> {
> // surprise errant coding here
> var docImages = obj.parentNode.getElementsByTagName("img");
> // more code follows
> }
>
> When I change the 'body' tag to the following, it works:
>
> <body onload="a_function(document.body);">
>
> Much to my surprise, the 'obj' parameter is NOT the 'body' node of the
> document (i.e., document.body). The object 'obj' is in fact an instance
> of a 'window' class (the window object). This behavior is true in Firefox
> (as seen in Venkman) and in MS Internet Explorer (as guessed by an error
> in script).
>
>
>
> Two and a half questions:
>
> 1. Why is 'obj' the window object and not document.body object (node)?
>
> 2. Is it proper to use lowercase arguments in the getElementsByTagName()
> method? Suggested failsafe workarounds?[/color]

Question 1

As I understand it, when you set onload for the body, this actually by
design sets onload for the window object.

http://msdn.microsoft.com/library/de...nts/onload.asp

I suspect, but do not know for sure, that this will also apply to
Mozilla and other.

Hence the "this" would be expected to refer to the window object.

It would be fairly easy to adjust your onload handler, or even take it
out of the body tag (as it may not validate) and do:-

window.onload=function()
{
var docImages = window.document.getElementsByTagName("img");
}

Question 2

For HTML4 documents, I think upper or lower case may be used, as the
tag names are not case sensitive so implementations of
getElementsByTagName should allow for you to use upper or lower case
names as an argument.

http://www.w3.org/TR/REC-html40/intr....html#idx-case

For XHTML documents, lower case must be used for tag names, as the
XHTML spec specifies lower case only tag names.

So for future proofing, I would probably stick to lower case.

Julian

  #4  
Old November 8th, 2005, 10:42 AM
Julian Turner
Guest
 
Posts: n/a
Default Re: No Body Node Upon Page Load?


Julian Turner wrote:[color=blue]
> For XHTML documents, lower case must be used for tag names, as the
> XHTML spec specifies lower case only tag names.[/color]

Sorry, having seeing the other reply, I should be more precise: lower
case for element names defined in the XHTML spec.

Julian

  #5  
Old November 8th, 2005, 10:42 AM
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
Default Re: No Body Node Upon Page Load?

Julian Turner wrote:
[color=blue]
> Julian Turner wrote:[color=green]
>> For XHTML documents, lower case must be used for tag names, as the
>> XHTML spec specifies lower case only tag names.[/color]
>
> Sorry, having seeing the other reply, I should be more precise:
> lower case for element names defined in the XHTML spec.[/color]

It's either "tag name" or "element type" to avoid confusion
with the `name' attribute of an ([X]HTML) element.


Regards,
PointedEars
  #6  
Old November 8th, 2005, 10:42 AM
Julian Turner
Guest
 
Posts: n/a
Default Re: No Body Node Upon Page Load?


Thomas 'PointedEars' Lahn wrote:
[color=blue]
> It's either "tag name" or "element type" to avoid confusion
> with the `name' attribute of an ([X]HTML) element.[/color]

Good point. Thank you.

Julian

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.