[Javascript] Problem w/Javascript, interpolation, functions, onclick
Peter-Paul Koch
pp.koch at gmail.com
Wed Oct 25 13:09:06 CDT 2006
> >On 10/25/06, Paul Novitski <paul at juniperwebcraft.com> wrote:
> >> // now act on the object whose behavior triggered
> >>this function
> >> var sClass = this.tagName; // etc.
> >
> >This is incorrect. In IE, events are executed in the window scope, so
> >when you do this.tagName, it is equivalent to doing window.tagName.
> >You must get the target element from the event itself
>
>
> According to PP, Matt, what you say is true if you're inserting your
> function calls in the HTML but I'm correct if applying the behavior
> through the DOM.
In the quoted function the this keyword refers to oSomething; ie. the
element you registered the event handler on.
<tag id="something">something to click</tag>
...
var oSomething = document.getElementById("something");
if (!oSomething) return;
oSomething.onclick = jsDoSomething; <-- event handler
registered on oSomething with the traditional model, in which this
works fine
...
function jsDoSomething(evt)
{
// cancel event-bubbling
if (evt) { event = evt; }
event.cancelBubble = true;
// now act on the object whose behavior triggered
this function
var sClass = this.tagName; // etc. <-- this
refers to oSomething
}
--
-------------------------------------------------------------------
ppk, freelance web developer
http://www.quirksmode.org/
------------------------------------------------------------------
More information about the Javascript
mailing list