SEO Forums: Your seo discussion forum  
Welcome, Unregistered.
You last visited: Today at 12:42 AM
Tags:



Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-26-2008, 04:38 AM
yawnmoth
Guest
 
Posts: n/a
Default onclick behaves differently when defined via javascript



http://www.frostjedi.com/terra/scrip...his-alert.html
http://www.frostjedi.com/terra/scrip...is-alert2.html

Why, when you click in the black box, do the alert boxes say different
things? Shouldn't they say the same thing?
Reply With Quote


  #2 (permalink)  
Old 09-26-2008, 04:38 AM
Jeremy J Starcher
Guest
 
Posts: n/a
Default Re: onclick behaves differently when defined via javascript

On Thu, 25 Sep 2008 15:45:50 -0700, yawnmoth wrote:

> http://www.frostjedi.com/terra/scrip...his-alert.html
> http://www.frostjedi.com/terra/scrip...is-alert2.html
>
> Why, when you click in the black box, do the alert boxes say different
> things? Shouldn't they say the same thing?


Your problem is here:
<div onclick="test()"> </div>

It is NOT the same as:
<div onclick=test> </div>

The first form runs the "test()" function within the global context. The
Global Object does not have a property called tagName.


The second form (as well as your second example) use a function
reference, and runs within the context of the div.

Understanding the environment the script runs in is essential to
understanding what "this" refers to.

Reply With Quote


  #3 (permalink)  
Old 09-26-2008, 04:25 PM
Henry
Guest
 
Posts: n/a
Default Re: onclick behaves differently when defined via javascript

When an intrinsic event attribute is provided for an element (assuming
it is recognised, etc.), such as:-

<div onclick="test()"> </div>

- the browser uses the string value (the "text()" in this case) as the
body text of a function that it creates and assigned to the
corresponding property of the representation of the element in the
DOM. So what the browser does here is the equivalent of:-

divRef.onclick = function(event){
test();
};

(with or without the - event - formal parameter, depending on the
browser)

The difference between the browser doing this and your doing the
equivalent of:-

divRef.onclick = test;

- is that when the browser calls the function it calls the function as
- divRef.onclick(); - (with or without an event object as the
argument, depending on the browser) so the - this - value for the
execution of the function assigned to - divRef.onclick - is a
reference to the DIV element, but the function assigned is different.
One is the function that the browser created (the function that will
then call - test -) and the other is test itself. In the event that
the function called is the one created by the browser then when it
calls - test - it does so in a way that will make the - this - value
inside that call be a reference to the global object.
Reply With Quote


  #4 (permalink)  
Old 09-26-2008, 04:26 PM
Henry
Guest
 
Posts: n/a
Default Re: onclick behaves differently when defined via javascript

On Sep 26, 12:44*pm, Jorge wrote:
> On Sep 26, 12:43*pm, Henry wrote:
>
>> When an intrinsic event attribute is provided for an element
>> (assuming it is recognised, etc.), such as:-

>
>> <div onclick="test()"> </div>

>
>> - the browser uses the string value (the "text()" in this case)
>> as the body text of a function that it creates and assigned to
>> the corresponding property of the representation of the element
>> in the DOM. So what the browser does here is the equivalent of:-

>
>> divRef.onclick = function(event){
>> * * test();

>
>> };

>
> Correct me if I'm mistaken, but I think I once read somewhere that
> it does an eval() of the text: like (in this case)
> eval('test()')... ?


I cannot tell whether you are mistaken in thinking that you once read
that somewhere, but it is not the case.

Reply With Quote


  #5 (permalink)  
Old 09-26-2008, 04:26 PM
Jorge
Guest
 
Posts: n/a
Default Re: onclick behaves differently when defined via javascript

On Sep 26, 12:43*pm, Henry <rcornf...@raindrop.co.uk> wrote:
> When an intrinsic event attribute is provided for an element (assuming
> it is recognised, etc.), such as:-
>
> <div onclick="test()"> </div>
>
> - the browser uses the string value (the "text()" in this case) as the
> body text of a function that it creates and assigned to the
> corresponding property of the representation of the element in the
> DOM. So what the browser does here is the equivalent of:-
>
> divRef.onclick = function(event){
> * * test();
>
> };
>


Correct me if I'm mistaken, but I think I once read somewhere that it
does an eval() of the text: like (in this case) eval('test()')... ?

--
Jorge.
Reply With Quote


  #6 (permalink)  
Old 09-26-2008, 08:31 PM
yawnmoth
Guest
 
Posts: n/a
Default Re: onclick behaves differently when defined via javascript

On Sep 25, 6:01*pm, Jeremy J Starcher <r3...@yahoo.com> wrote:
> On Thu, 25 Sep 2008 15:45:50 -0700, yawnmoth wrote:
> >http://www.frostjedi.com/terra/scrip...his-alert.html
> >http://www.frostjedi.com/terra/scrip...is-alert2.html

>
> > Why, when you click in the black box, do the alert boxes say different
> > things? *Shouldn't they say the same thing?

>
> Your problem is here:
> <div onclick="test()"> </div>
>
> It is NOT the same as:
> <div onclick=test> </div>
>
> The first form runs the "test()" function within the global context. *The
> Global Object does not have a property called tagName.
>
> The second form (as well as your second example) use a function
> reference, and runs within the context of the div.
>
> Understanding the environment the script runs in is essential to
> understanding what "this" refers to.


Why, then, don't any of these work?:

http://www.frostjedi.com/terra/scrip...is-alert3.html
http://www.frostjedi.com/terra/scrip...is-alert4.html
http://www.frostjedi.com/terra/scrip...is-alert5.html

I can understand the first one. The first one doesn't, presumably,
work for the same reason that "x=y; y=2;" doesn't result in x equaling
2 - ie. test hasn't been defined and so onclick is set to undefined.

But what about the second and third ones? I tried the third one since
your "It is NOT the same as" didn't include quote marks.
Reply With Quote


  #7 (permalink)  
Old 09-27-2008, 07:22 AM
Jorge
Guest
 
Posts: n/a
Default Re: onclick behaves differently when defined via javascript

On Sep 26, 1:58*pm, Henry <rcornf...@raindrop.co.uk> wrote:
>
> I cannot tell whether you are mistaken in thinking that you once read
> that somewhere, but it is not the case.


:-)

Still, there's something weird here, something that seems to be
against your theory, see:

<html>
<head>
</head>
<body onload= "alert((document.body.onload === arguments.callee)+'\r
\n'+arguments.callee)">
</body>
</html>

how do you explain this ?

--
Jorge.

Reply With Quote


  #8 (permalink)  
Old 09-27-2008, 07:22 AM
Jorge
Guest
 
Posts: n/a
Default Re: onclick behaves differently when defined via javascript

On Sep 26, 9:58*pm, Jorge <jo...@jorgechamorro.com> wrote:
>
> Still, there's something weird here, something that seems to be
> against your theory, see:
>
> <html>
> <head>
> </head>
> <body onload= "alert((document.body.onload === arguments.callee)+'\r
> \n'+arguments.callee)">
> </body>
> </html>
>
> how do you explain this ?
>


The link: http://jorgechamorro.com/cljs/018/

--
Jorge.
Reply With Quote


Reply



Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 12:42 AM.

Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
Great Seo Blog at SEONOTEPAD.COM