No Body Node Upon Page Load?

P

Patient Guy

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

Thomas 'PointedEars' Lahn

Patient said:
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).

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.
Two and a half questions:

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

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.
2. Is it proper to use lowercase arguments in the getElementsByTagName()
method?

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

Suggested failsafe workarounds?

All lowercase and uppercase combinations of the element type.


PointedEars
 
J

Julian Turner

Patient said:
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?

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/default.asp?url=/workshop/author/dhtml/reference/events/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/intro/sgmltut.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
 
J

Julian Turner

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

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

Julian
 
T

Thomas 'PointedEars' Lahn

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

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


Regards,
PointedEars
 
J

Julian Turner

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

Good point. Thank you.

Julian
 

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

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top