Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   assigning event with attachEvent instead of onmouseover (http://www.velocityreviews.com/forums/t936162-assigning-event-with-attachevent-instead-of-onmouseover.html)

webgour 06-10-2008 03:01 PM

assigning event with attachEvent instead of onmouseover
 
Hello,

I'm having difficulty using attachEvent instead of simply assigning to
mouseover in my object Sample03. When i use myImage.onmouseover =
this.showmouseover(); in the following all works fine, if I replace it by
myImage.attachEvent('onmouseover',this.showmouseov er()); I get for the
alert(this.id) "undefined" and not "gwlogo"

<html>
<head>
</head>
<SCRIPT language="javascript" type="text/javascript">
var Sample03 = function(){
this.Property01 = "Prop01";
var myImage = document.getElementById("gwlogo");
myImage.onmouseover = this.showmouseover();
//myImage.attachEvent('onmouseover',this.showmouseov er());
}
Sample03.prototype.showmouseover = function()
{
var _this = this;
return(
function(e){
alert(this.id);
alert(_this.Property01);
})
}
window.onload=function(){
var mySample = new Sample03();
}

</SCRIPT>
<body>
<img src="http://localhost/winxp.gif" id="gwlogo">
</body>
</html>

Thank you for your assistance.












Holger Jeromin 06-10-2008 03:44 PM

Re: assigning event with attachEvent instead of onmouseover
 
webgour schrieb am 10.06.2008 17:01:

> I'm having difficulty using attachEvent instead of simply assigning to
> mouseover in my object Sample03. When i use myImage.onmouseover =
> this.showmouseover(); in the following all works fine, if I replace it by
> myImage.attachEvent('onmouseover',this.showmouseov er()); I get for the
> alert(this.id) "undefined" and not "gwlogo"


http://www.quirksmode.org/js/events_advanced.html

"The event handling function is referenced, not copied, so the this
keyword always refers to the window and is completely useless."

--
Mit freundlichen Grüßen
Holger Jeromin

Álvaro G. Vicario 06-10-2008 04:07 PM

Re: assigning event with attachEvent instead of onmouseover
 
webgour escribió:
> I'm having difficulty using attachEvent instead of simply assigning to
> mouseover in my object Sample03. When i use myImage.onmouseover =
> this.showmouseover(); in the following all works fine, if I replace it by
> myImage.attachEvent('onmouseover',this.showmouseov er()); I get for the
> alert(this.id) "undefined" and not "gwlogo"


Unless it's just an exercise, I suggest you grab a third-party library
or framework to attach events. Apart from the headaches it'll save, it
won't be IE only. I've often used the code described here (find the
"downloadable version" link):

http://dean.edwards.name/weblog/2005/10/add-event2/

> <SCRIPT language="javascript" type="text/javascript">


The language attribute is deprecated.


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--

Thomas 'PointedEars' Lahn 06-12-2008 10:40 PM

Re: assigning event with attachEvent instead of onmouseover
 
Holger Jeromin wrote:
> webgour schrieb am 10.06.2008 17:01:
>> I'm having difficulty using attachEvent instead of simply assigning to
>> mouseover in my object Sample03. When i use myImage.onmouseover =
>> this.showmouseover(); in the following all works fine, if I replace it by
>> myImage.attachEvent('onmouseover',this.showmouseov er()); I get for the
>> alert(this.id) "undefined" and not "gwlogo"

>
> http://www.quirksmode.org/js/events_advanced.html
>
> "The event handling function is referenced, not copied, so the this
> keyword always refers to the window and is completely useless."


See also:

http://www.quirksmode.org/blog/archi...nt_consid.html


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7@news.demon.co.uk>

RobG 06-13-2008 02:39 AM

Re: assigning event with attachEvent instead of onmouseover
 
On Jun 11, 1:44 am, Holger Jeromin <news03_2...@katur.de> wrote:
> webgour schrieb am 10.06.2008 17:01:
>
> > I'm having difficulty using attachEvent instead of simply assigning to
> > mouseover in my object Sample03. When i use myImage.onmouseover =
> > this.showmouseover(); in the following all works fine, if I replace it by
> > myImage.attachEvent('onmouseover',this.showmouseov er()); I get for the
> > alert(this.id) "undefined" and not "gwlogo"

>
> http://www.quirksmode.org/js/events_advanced.html
>
> "The event handling function is referenced, not copied, so the this
> keyword always refers to the window and is completely useless."


The line above that on Quirksmode is not quite correct, it says (in
regard to the IE event model):

"Events always bubble, no capturing possibility."


When it should say:

"Events *only* bubble..."


Not all events bubble, and some events that bubble in other browsers
do not bubble in IE (such as the change event for form elements).


--
Rob


All times are GMT. The time now is 02:54 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.