assigning event with attachEvent instead of onmouseover

Discussion in 'Javascript' started by webgour, Jun 10, 2008.

  1. webgour

    webgour Guest

    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.
    webgour, Jun 10, 2008
    #1
    1. Advertising

  2. 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."

    --
    Mit freundlichen Grüßen
    Holger Jeromin
    Holger Jeromin, Jun 10, 2008
    #2
    1. Advertising

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


    --
    -- 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
    --
    Álvaro G. Vicario, Jun 10, 2008
    #3
  4. 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.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."


    See also:

    http://www.quirksmode.org/blog/archives/2005/08/addevent_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$>
    Thomas 'PointedEars' Lahn, Jun 12, 2008
    #4
  5. webgour

    RobG Guest

    On Jun 11, 1:44 am, 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.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."


    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
    RobG, Jun 13, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PedroVasconcelos

    Problem with AttachEvent

    PedroVasconcelos, Feb 21, 2004, in forum: Javascript
    Replies:
    5
    Views:
    92
    Lasse Reichstein Nielsen
    Feb 21, 2004
  2. weston
    Replies:
    1
    Views:
    238
    Richard Cornford
    Sep 22, 2006
  3. J
    Replies:
    5
    Views:
    110
  4. wolverine
    Replies:
    0
    Views:
    100
    wolverine
    Sep 27, 2007
  5. wolverine
    Replies:
    3
    Views:
    104
    wolverine
    Sep 28, 2007
Loading...

Share This Page