1

I have a fairly simple ASP.NET page that renders an HTML input (text box). Within the generated HTML, I attach a handler to several events, including onfocus, onkeypress, and onkeyup. Because this is a solution targeted at a version of IE that does not support addEventListener (situation about which I can do nothing), I am forced to use attachEvent.

A typical call to attachEvent is as follows - I've excerpted this source from the original for the sake of brevity/clarity, so it is not precisely the code at issue:

var hostControl = document.getElementById('mytextbox');
var attachResult = hostControl.attachEvent('onfocus', function(){
    hostControl.select();
});

if (!attachResult)
{
    alert('Attach failed.');
}

attachResult = hostControl.attachEvent('onblur', function(){
    if (hostControl.value=='') 
    {
        alert('Warning - no entry.');
    }
});

if (!attachResult)
{
    alert('Attach failed.');
}

When I step through this code in the IE debugger, attachEvent returns 'true' in both instances, which should indicate that the event attachment attempt was successful. However, when I look at the [Event] handlers for the control within the debugger, all the events show 'null', no handler attached.

Things I've tried/researched:

  • I've read several different articles on the vagaries of event attachment in IE, so I've speciously avoided any 'this' references.
  • I tried one version that used one of the addEvent wrapper blocks that tries to use addEventListener if available, even though I knew this would be an IE solution.

When I tried that version against FireFox, event attachment worked properly through addEventListener, but failed in IE using attachEvent (with attachEvent still returning true).

  • I then opted to eliminate any possible problems the wrapper might be introducing, and used attachEvent directly against the control, which leads me where I am now. The problem persists.

I would like to think I've simply overlooked something very simple, as I've hooked up events before without difficulty, but something here is throwing me a curveball I just don't recognize. Appreciate the extra eyeballs on this to see where I've erred.

David W
  • 10,062
  • 34
  • 60
  • Why are you still using `attachEvent`? – gen_Eric Apr 11 '13 at 15:49
  • Because the solution is targeted for a customer installation of IE that does not support addEventListener. Thought I had mentioned that in the first paragraph. – David W Apr 11 '13 at 15:50
  • Yeah, you did mention that. Sorry :-) – gen_Eric Apr 11 '13 at 15:51
  • No problem! I appreciate the interest. – David W Apr 11 '13 at 15:54
  • I just tested the code you posted in IE8, and it works fine: http://jsbin.com/amuqug/1 – gen_Eric Apr 11 '13 at 15:56
  • You say the events in the debugger show `NULL`... which debugger, and how are you looking at the `[Event]` handlers? Doe debugging events, I like to use: http://www.sprymedia.co.uk/article/visual+event+2 – gen_Eric Apr 11 '13 at 15:58
  • From two different venues - the IE8 script debugger, and the integrated Visual STudio 2010 debugger. The references show null when expanding the [EVENTS] references when the debugger is stopped on the code immediately following the attachEvent call. The lack of event attachment is further demonstrated by the absence of the desired behavior following the subject events. – David W Apr 11 '13 at 16:00
  • It works for me in IE 8 (http://jsbin.com/amuqug/1). Is there any *other* code on the page that could be editing the events? Did you make sure to run this code *after* the DOM is ready? – gen_Eric Apr 11 '13 at 16:02
  • Man, this is frustrating. I will have to dig into the page further to see if there is some oddness going on with the DOM I'm not aware of. Shouldn't be this complicated :) AFAIK, the DOM should be ready, but at this point, I'm not sure of anything. – David W Apr 11 '13 at 16:09
  • It's IE, of course it's frustrating. Their hacked up APIs and sub-par dev tools aren't supposed to be your friend. I feel your pain man, I really do. I had to deal with this crap too. All I can think of is that your error is elsewhere. You sure you don't have any trailing commas or something? :-P Do you think you could put up a live example of your code with the problem occurring? Then I may be able to figure out what's going on, now I can only guess >.> – gen_Eric Apr 11 '13 at 16:13
  • Hey, I appreciate the effort. In some way, I'm kinda glad it wasn't something just exceedingly stupid on my part that someone could answer in about 5 seconds :) Thanks again. I'll check for stray commas and such! – David W Apr 11 '13 at 16:14
  • Unless it *is* something exceedingly stupid and *I* just don't see it either ^_^ – gen_Eric Apr 11 '13 at 16:15
  • 1
    Okay, I think I found the problem, and its exceedingly depressing. Rocket, you were on the right track about the DOM not being complete. Turns out that some other code on the page initializes an Ajax function that references the text box, and kills all the event wireup code. * sigh *. Thanks for the help, everyone. Now I have to see if its even possible to work around this. – David W Apr 11 '13 at 16:23
  • Can you bind your events *after* the AJAX function does? – gen_Eric Apr 11 '13 at 16:25
  • That's my very next step. My eyeballs are teabags now, so that's happening after lunch :) – David W Apr 11 '13 at 16:27
  • Glad you figured it out! I've almost pulled my hair out over similar issues before :-) – gen_Eric Apr 11 '13 at 16:30

0 Answers0