By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,453 Members | 937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,453 IT Pros & Developers. It's quick & easy.

assigning event with attachEvent instead of onmouseover

P: n/a
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.



Jun 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Jun 27 '08 #2

P: n/a
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
--
Jun 27 '08 #3

P: n/a
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.showmouseo ver()); 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, <f8*******************@news.demon.co.uk>
Jun 27 '08 #4

P: n/a
On Jun 11, 1:44 am, Holger Jeromin <news03_2...@katur.dewrote:
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
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.