On 28 Jan 2005 09:42:40 -0800, <va********@hot mail.com> wrote:
Very simple code. Why won't this work?
[snip]
window.onload = document.getEle mentById("thisT hing"
).style.backgro und = "red";
When you assign a listener to an element via a script (rather than in
HTML), the listener expects a function reference. What you've done above
is attempt[1] to assign the string, 'red', to the window.onload and
style.backgroun d properties.
Although you can specify free-form code in HTML attributes, what the user
agent does is place that code in an anonymous function and assigns it to
the element. Therefore,
<body onload="/* ... */">
becomes
window.onload = function(event) {
/* ... */
};
except in IE (it doesn't pass an event argument - the event object is
global).
You have to perform this step yourself.
[snip]
Hope that helps,
Mike
[1] It is nothing more than an attempt as accessing the style property
will cause an error because as far as the user agent is concerned the
element, thisThing, doesn't exist yet. The user agent must at least
encounter and parse the element before that can change.
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.