Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts

    expando context "this" discrepancy in IE

    Inside an expando event handler, "this" is always a reference to the object:
    Code:
    someDiv.onmousedown = function()
    {
        //"this" is a reference to someDiv
    
    };
    So by extension:
    Code:
    someDiv.addEventListener('mousedown', function()
    {
        //"this" is a reference to someDiv
    
    }, false);
    However in IE:
    Code:
    someDiv.attachEvent('onmousedown', function()
    {
        //"this" is a reference to window
    
    });
    Have I got that right, is that what always happens? Or have I encountered an anomoly within the script I'm using it?
    Last edited by brothercake; 03-05-2005 at 08:47 PM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    If you are calling someDiv.attachEvent, then you can use the identifer "someDiv" within the function you are passing to attachEvent.

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ta
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh ... no, that's not going to work .. because the "someDiv" reference is actually "this.elements[i]", and even though I have a parent-"this" reference created as "self = this", so I can go "self.elements[i]" - it doesn't come out as the element I want, it comes out as that element's nth element (depending on the residual value of i from the constructing loop)

    Phew ... nevermind - I have an alternative hack:
    Code:
    //convert event argument
    if(!e) { e = window.event; }
    
    //if "this" is a reference to window (IE)
    if(this == window)
    {
    	//iterate upwards from the event target 
    	//until we find a reference to the box
    	var tmp = e.srcElement;
    	var parent = tmp.parentNode;
    	while(!/dbx\-box/.test(parent.className))
    	{
    		tmp = parent.parentNode;
    	}
    }
    
    //otherwise copy this to tmp
    else { tmp = this; }
    Not the most elegant ... but it will have to do for now.

    Cheers anyway
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    this contains the global object in all cases where the function is not called as a method of an object. The DOM0 event handlers worked through method calls.

    addEventListener if implemented according to the rules for function calls laid forth by the ECMAScript 3 specification, uses function callbacks and not method calls and similarly goes for attachEvent. (Specifically for the reason that using method calls disallow multiple event listeners...) Thus, if the ECMAScript implementation is reasonably conformant, this SHOULD point to the global object, not doing so would be an error. For W3C DOM events, the Event.prototype.currentTarget property should contain a reference to the element which originally dispatched the event, which means that it should be the same as this for a DOM0 event.

    Sadly, the Microsoft model only has Event.prototype.srcElement which is equivalent to Event.prototype.target and not any equivalent to Event.prototype.currentTarget.



    From previous testing, I know that there are W3C DOM implementations (though I can't recall exactly which browsers, it was a while ago I tested this) that act like they are callbacks as they should, and there are those that act like they were method calls, so you shouldn't rely on this pointing to the dispatching node unless you are actually using the DOM0 event mechanism.

    Edit: Edited to take some things discussed in DOM Event Scripting, (undocumented) Tip #1: DOM2 Events and Internet Explorer into account, and for correctness.
    Last edited by liorean; 05-25-2005 at 05:47 AM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Righto .. I'll use upward iteration from target for everyone then. Cheers
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •