[Javascript] Problem w/Javascript, interpolation, functions, onclick

Paul Novitski paul at juniperwebcraft.com
Wed Oct 25 13:05:09 CDT 2006


At 10/25/2006 10:44 AM, Matt Warden wrote:
>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:
________________________

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 




More information about the Javascript mailing list