self.parent vs. parent?

David Faure faure at kde.org
Thu Mar 10 18:00:01 GMT 2005


On Thursday 10 March 2005 12:27, Jason Keirstead wrote:
> For example, change your test case to this:
> 
> <script>
> function alertTag() {
>          alert( this );
>          alert( this.tagName );
>          alert( tagName );
> }
> 
> document.getElementById('inp').onclick = alertTag;
> </script>
> <input type="text" id="inp2" value="second" onclick="alertTag()"/>
> 
> You will see that the first case functions as normal - the second case will 
> have the window as 'this'.

Indeed. After talking with Harri about all this I understand better how the scope
is determined - it's set when creating the function, not when calling it (unlike "this").

I was able to fix this bug now, in HEAD (ecma/kjs_events.cpp   1.93).

> I am actually quite surprised KHTML pushes scope like this in event handlers- 
> I have never seen a browser do that. 

Well we have to push it for the onclick="alert(tagName)" case.
It's just that we pushed it in both cases, which was indeed wrong. It now only
happens for event listeners set with HTML attributes.

> You normally have to get the context of 
> the event through e.target / e.currentTarget / e.srcElement.

Yes, because you can't pass an argument when doing foo.onclick=myfunction.
But when using HTML you could do onclick="myfunction(this)".

Thanks for the report, it was an interesting one to fix :)

-- 
David Faure, faure at kde.org, sponsored by Trolltech to work on KDE,
Konqueror (http://www.konqueror.org), and KOffice (http://www.koffice.org).




More information about the kfm-devel mailing list