self.parent vs. parent?

Jason Keirstead jason at keirstead.org
Thu Mar 10 11:27:44 GMT 2005


On March 9, 2005 07:08 pm, David Faure wrote:
> This shows (in Moz and IE) that there is a difference between those two
> ways. Both onclick event handlers have "this" set to the element being
> clicked, but only the second input has "this" in the scope (i.e. tagName
> resolves). In the first case (the function()), "this" isn't in the scope.

The reason that 'this' should be the scope in the second case, is that the JS 
string is not a function - it is an anonymous block that will be eval()'ed in 
the context of the node when the event fires.

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'.

I am actually quite surprised KHTML pushes scope like this in event handlers- 
I have never seen a browser do that.  You normally have to get the context of 
the event through e.target / e.currentTarget / e.srcElement.

PS - I filed a bug on this yesterday - 
http://bugs.kde.org/show_bug.cgi?id=101202

-- 
Jason Keirstead
http://www.keirstead.org




More information about the kfm-devel mailing list