assigning event with attachEvent instead of onmouseover

W

webgour

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.showmouseover()); 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.showmouseover());
}
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.
 
H

Holger Jeromin

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.showmouseover()); 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."
 
Á

Álvaro G. Vicario

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.showmouseover()); 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.
 
R

RobG

webgour schrieb am 10.06.2008 17:01:


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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top