szimek wrote:
>
> > Hi,
>
> > I've got this very simple code:
>
> > <body onclick="foo(event, this)">
>
> > Inside foo method in IE "this" is body object, but in FF it's window
> > object - at least that's what MS Script Editor and Firebug are saying.
> > Any ideas why it works like that?
What does your foo declaration look like? It should be something like:
function foo(e, el) {...}
Where "e" will be a reference to the event object and el will be a
reference to the object referenced by the this keyword in the caller,
which will be the body element in all browsers I know of.
I'll guess that you have something like:
function foo(event, this) {...}
or maybe:
function foo() {
// do stuff with event
// do stuff with this
}
both of which are not going to work the way you want in browsers
compliant with the W3C DOM level 2 such as Firefox and others.
> I've posted it too soon. Basically it really sucks:
>
> "Attaching a function using addEventListener() changes the value of
> this--note that the value of this is passed to a function from the
> caller."
> "If the onclick handler is added in the HTML source then value of this
> when called from the onclick event will be a reference to the global
> (window)
object."
http://developer.mozilla.org/en/docs/DOM:element.addEventListener
You've taken that completely out of context. Read the example again, it
is quite different to what you posted.
> Why does it work like that?! I'm rewritting IE-only app that has
> inline javascript everywhere thus "this" element is wrong in almost
> every event handler...
I think you have misunderstood what is going on.
> 2 questions
> - is there a different way than using addEventHandler()
I think you mean addEventListener...
> to modify FF
> behavior to make "this" actual element instead of window object?
Use it like this:
<script type="text/javascript">
function foo(e, el) {
// e is a reference to the event object
// el is a reference to the element that called the function
}
</script>
<body onclick="foo(event, this);">...</body>
> - if I'm forced to use addEventHandler
You aren't. There are more issues using addEventListener/attachEvent
than using in-line code.
--
Rob
"We shall not cease from exploration, and the end of all our
exploring will be to arrive where we started and know the
place for the first time." -- T. S. Eliot