SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    removeEventListener not working

    Hello again.

    I'm looping through a group of anchors (grouped via class) that I want to respond normally (i.e. go to their href) in some cases and to perform some JS in others:
    Code:
    function init(what){
      anchors = $c('showbox'); // $c is a getElementsByClassName sort of thing
      for (var i = 0; i < anchors.length; i++) {
        anchor = anchors[i];
        if (what == 'start') anchor.addEventListener('click', begin, false);
        if (what == 'kill') anchor.removeEventListener('click', begin, false);
      }
      function begin(e) {
        // lots of javascript
      }
    }
    The 'start' and 'kill' business come from two radio buttons:
    Code:
    var rads = $n('how'); // $n = getElementsByName
      for (var i = 0; i < rads.length; i++) {
        rads[i].onchange = function() {
        if ((this.value == 'nobox' || this.value == 'box') && this.checked == true) {
          if (this.value == 'box') init('start');
          if (this.value == 'nobox') init('kill');
          }
        }
      }
    The code works well to set the event listener, but it doesn't remove it. Any thoughts?

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Raffles,

    A few suggestions...

    1. I'm not 100&#37; sure about this, but... Registering the 'begin' function as a listener creates a closure on the 'init' function. It is a different closure each time 'init' is called - so it is possible that 'begin' is actually a different function reference each time. That would explain why you can't remove the listener - because it is different from the listener you added! So my suggestion is to move the 'begin' function outside of the 'init' function.

    2. You do know that addEventListener and removeEventListener are not implemented in IE?

    3. Another suggestion: I would be careful about using 'anchors' as an identifier. "document.anchors" is an existing object.

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Mike,

    1. That did it, thanks.

    2. Yes, I'm just concentrating on getting it to function properly first, then add the necessary fixes. Those functions do exist in IE7 though, right?

    3. Thanks, I'll change that. Presumably it's an array of all anchors in the document, like document.images? I wonder, with things like document.getElementsByTagName, it makes them somewhat unnecessary - are they going to become deprecated?

    EDIT: I just forgot something fairly crucial. In my 'begin' function, I need to use 'this'. However, window.attachEvent doesn't pass it on. What is the 'accepted' way to get around this?

  4. #4
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The DOM2 event model is still not supported by IE - not even IE7. But since you need access to 'this' I would suggest:
    Code:
        if (what == 'start') anchor.onclick = begin;
        if (what == 'kill') anchor.onclick = null;
    And this is as cross-browser as you can get

  5. #5
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Cheers Mike, Occam's razor in action.

    It doesn't cease to impress me how rubbish Microsoft's browser is. I think the only good things about it over other browsers are onmouseenter and onmouseleave. I suppose innerHTML is quite nice too, so I'll give them credit for coming up with that first.

    Did IE7 update its javascript at all?

  6. #6
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did IE7 update its javascript at all?
    I'm not really sure - but I think most of the changes were cosmetic (tabs) and CSS-related. Oh yeah... IE7 now has a native XMLHttpRequest object.


    Hey, you're about to hit 1000 posts

  7. #7
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Oh yes, of course - Ajax without ActiveX. I remember now, I read something on the IEBlog about how that was pretty much all they were going to do because they were so busy catching up with 6 years' worth of CSS.

    Hey, I have 6 orange things now.


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
  •