473,594 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

No Body Node Upon Page Load?


I have this in a document:

<body onload="a_funct ion(this);">

In an (external) script file, I define a_function() as follows:
function a_function(obj)
{
// surprise errant coding here
var docImages = obj.parentNode. getElementsByTa gName("img");
// more code follows
}

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

<body onload="a_funct ion(document.bo dy);">

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 getElementsByTa gName()
method? Suggested failsafe workarounds?
Nov 8 '05 #1
5 4983
Patient Guy wrote:
I have this in a document:
<body onload="a_funct ion(this);">

In an (external) script file, I define a_function() as follows:
function a_function(obj)
{
// surprise errant coding here
var docImages = obj.parentNode. getElementsByTa gName("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(t his)">

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(thi s)">

would result in

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

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

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

(proprietary) or

document.body.a ddEventListener ("load", new Function("foo(t his);"), 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 getElementsByTa gName()
method?
Yes, as long as a non-XML document type is concerned.

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


All lowercase and uppercase combinations of the element type.
PointedEars
Nov 8 '05 #2

Patient Guy wrote:
I have this in a document:

<body onload="a_funct ion(this);">

In an (external) script file, I define a_function() as follows:
function a_function(obj)
{
// surprise errant coding here
var docImages = obj.parentNode. getElementsByTa gName("img");
// more code follows
}

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

<body onload="a_funct ion(document.bo dy);">

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 getElementsByTa gName()
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/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=f unction()
{
var docImages = window.document .getElementsByT agName("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
getElementsByTa gName 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

Nov 8 '05 #3

Julian Turner wrote:
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

Nov 8 '05 #4
Julian Turner wrote:
Julian Turner wrote:
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.


It's either "tag name" or "element type" to avoid confusion
with the `name' attribute of an ([X]HTML) element.
Regards,
PointedEars
Nov 8 '05 #5

Thomas 'PointedEars' Lahn wrote:
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

Nov 8 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
7677
by: Keith M | last post by:
Hi, I have found a very useful piece of code in the msdn which shows how to load an xml config file, search for a particular value and replace it. Could someone possibly convert it to C# from the VB I have? I also would like to use similar code to update the machine.config file so if someone could explain how to add a node ("appSettings") if node not found and also how to add a key value pair into the "appSettings" node if found I would...
7
6206
by: Calvin KD | last post by:
Hi everyone, Has someone out there experienced the same problem? What i wanted to do is to display a popup window (modal) upon the loading of a webpage. Here is my code sample (in html view): <HTML> <HEAD> .... </HEAD> <BODY onload="ShowPopUp();" MS_POSITIONING="GridLayout"> ....
1
8727
by: lwhitb1 | last post by:
I have been trying to load a javascript function from the body onload html tag, but I only want the function to load the first time the page is loaded: I have investigated but haven't found anything that works.. I thought about cookies, but what if the user's disable them? My code: //I want to call the Toggle function below only on the 1st time the page is loaded:
23
3063
by: Big Bill | last post by:
http://www.promcars.co.uk/pages/bonnie.php I don't believe they should be there, can I take them out without stopping the includes from functioning? I'm the (hapless) optimiser on this one... I have to correct where they've spelled my name wrong too...sigh... BB --
10
5651
by: Josselin | last post by:
(Mac 10.4.6 , Firefox/1.5.0.3 Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; fr; rv:1.8.0.3) Gecko/20060426 ) I am trying to setup a Reusable Image Cache in JS ( as per http://www.devx.com/webdev/Article/20947 ) In order to o survey the events from image loading in a group container, It seems necessary to perform the following function, ImagePair(file) . Unfortunatly Firefox javascript checking mentions an error with this
10
6256
by: VK | last post by:
Richard Cornford wrote: > The html element is the outermost element in an (x)HTML document. > All other elements are its descendants, with its direct children being > the head and body elements. It is not exactly true at least for Firefox (keep missing to test in full on Opera). In Firefox HTML is a container as any other where you can add say images and div's. Try html { width:100%;
5
3670
by: John Kotuby | last post by:
Hi all, My application consists of a MasterPage which is used for all WebForms. I then create UserControls to populate the content area for the different WebForms. In one complicated UserControl I have a number of Javscript functions. Everything on the page works fine, until I try to add the OnLoad event attribute to the Body tag which is generated by the Master Page.
2
3906
by: John Kotuby | last post by:
Hi guys, I am converting a rather complicated database driven Web application from classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The original ASP application works quite well, so at times it is tempting just to port parts of it over mostly as-is. In fact, one MSDN article I read suggested using straight HTML wherever possible to make the app more efficient and less resource demanding. On one page there are 2...
4
1823
Claus Mygind
by: Claus Mygind | last post by:
I have been working with the TreeWalker. I want to load a page from another domain. I got that working fine. I store window.open() object in a global variable winRef. Then I would like to walk the nodes and find an anchor that contains "seek/log" in it's href attribute. Can that be done or is that a security violation and not permitted? I load the winRef variable like this winRef = window.open("http://www.otherSite.com" );
0
7946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7877
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8253
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8009
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6661
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5739
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5411
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3867
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1482
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.