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