SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    "this" keyword - IE problem

    An event listener call this function:

    Code:
    clickListener: function()
    {
      selectedRow = this.nodeName;
    }
    In internet explorer 7 "this" return "undefined". Exist a solution to solve this problem ? Firefox work great!

    Thanks!

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    clickListener: function()
    {
    selectedRow = this.nodeName ||
    window.event? event.srcElement.nodeName: '';
    }

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    That's an interesting way of doing it.
    A more standard way that can be used in a wide variety of situations is to use an addEvent function that retains the this keyword in for IE events.

    Some of the better addEvent functions are from
    http://ejohn.org/projects/flexible-javascript-events/
    http://www.dustindiaz.com/rock-solid-addevent/

    Code javascript:
    // from [url]http://ejohn.org/projects/flexible-javascript-events/[/url]
    function addEvent( obj, type, fn ) {
      if ( obj.attachEvent ) {
        obj['e'+type+fn] = fn;
        obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
        obj.attachEvent( 'on'+type, obj[type+fn] );
      } else
        obj.addEventListener( type, fn, false );
    }
     
    clickListener: function() {
        selectedRow = this.nodeName;
    }
     
    addEvent(el, 'click', obj.clickListener);
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I need to use extensions in future projects. Javascript without extensions is pretty stupid due to explorer.


Bookmarks

Posting Permissions

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