Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Javascript] Problem w/Javascript, interpolation, functions, onclick
- From: paul at juniperwebcraft.com (Paul Novitski)
- Subject: [Javascript] Problem w/Javascript, interpolation, functions, onclick
- Date: Wed Oct 25 13:06:14 2006
At 10/25/2006 10:44 AM, Matt Warden wrote: >On 10/25/06, Paul Novitski <paul@xxxxxxxxxxxxxxxxxxx> 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: ________________________ http://www.quirksmode.org/js/this.html In JavaScript this always refers to the "owner" of the function we're executing, or rather, to the object that a function is a method of. When we define our faithful function doSomething() in a page, its owner is the page, or rather, the window object (or global object) of JavaScript. An onclick property, though, is owned by the HTML element it belongs to. ... "However, if you use inline event registration <element onclick="doSomething()"> you do not copy the function! Instead, you refer to it, and the difference is crucial. The onclick property does not contain the actual function, but merely a function call: doSomething(); So it says "Go to doSomething() and execute it." When we arrive at doSomething() the this keyword once again refers to the global window object and the function returns error messages. ________________________ Regards, Paul
- Follow-Ups:
- [Javascript] Problem w/Javascript, interpolation, functions, onclick
- From: Peter-Paul Koch
- [Javascript] Problem w/Javascript, interpolation, functions, onclick
- References:
- [Javascript] Problem w/Javascript, interpolation, functions, onclick
- From: Nathan V. Patwardhan
- [Javascript] Problem w/Javascript, interpolation, functions, onclick
- From: Matt Warden
- [Javascript] Problem w/Javascript, interpolation, functions, onclick
- From: Nathan V. Patwardhan
- [Javascript] Problem w/Javascript, interpolation, functions, onclick
- From: Paul Novitski
- [Javascript] Problem w/Javascript, interpolation, functions, onclick
- From: Matt Warden
- [Javascript] Problem w/Javascript, interpolation, functions, onclick
- Prev by Date: [Javascript] Problem w/Javascript, interpolation, functions, onclick
- Next by Date: [Javascript] Problem w/Javascript, interpolation, functions, onclick
- Previous by thread: [Javascript] Problem w/Javascript, interpolation, functions, onclick
- Next by thread: [Javascript] Problem w/Javascript, interpolation, functions, onclick
- Index(es):