0

my events work great in everything but !@#$ IE. i found that i need to call the fireEvent('event'); function, but i don't know where to put it. to pull from a previous question i asked, is this where i put it?

$('field').addEvents ({
  'focus' : function() {
    this.fireEvent('focus');
    // do some stuff 
  }
  'blur' : function() {
    this.fireEvent('blur');
    // do other stuff 
  }}
});

because if it is... something else is wrong because i tried it and it doesn't work worth a tinker's damn.

can we just flat out tell people "access denied. you're using a retarded browser. get a real one and come back."? why can't Anonymous target the IE dev team?? :P

anyway. TIA.

WR!

WhiteRau
  • 818
  • 14
  • 32

1 Answers1

2

what you have here is redundant and will actually cause an infinite loop. it's essentially a self-invoking function. modern JIT compilers may go, 'oups, you probably did not mean to do that' and break the cycle but IE7/8 will likely just run it as written.

basically, there is a difference between a callback added by addEvent, which goes off on the native event firing - and manually firing off the callback (fireEvent).

later on - if you did $("field").fireEvent("focus") - it will just run the code in the bound function. it won't actually focus on the field. The native $("field").focus(); will do BOTH.

a caveat in using fireEvent is when your callback tries to do stuff with the event itself. eg, in this code:

el.addEvent("click", function(e) {
    e.stop();

    // do something
});

// later...

el.fireEvent("click"); // exception as e is not defined and stop is not a function

to work around this, you need to check if the argument e is defined and if stop is defined in the callback:

el.addEvent("click", function(e) {
    e && e.stop && e.stop();

    // do something
});

or you can pass a dummy object to fireEvent:

el.fireEvent("click", { 
    stop: Function.from
});

or even...

el.fireEvent("click", new DOMEvent()); // 1.4+, new Event() before.... 

Not sure if this helps as you had a lot of excess in your 'question' but it may give you an idea about how events are being used.

have fun

Community
  • 1
  • 1
Dimitar Christoff
  • 26,147
  • 8
  • 50
  • 69
  • i had no clue how to use the fireEvent method, so i was just guessing. i took the working block of code i was using and plopped the fireEvent where i thought it was supposed to go, not knowing exactly what the hell is was supposed to do. :P what made my eyebrows pop off my face was when i read [here](http://mootools.net/docs/core/Element/Element.Event) that "Internet Explorer fires element events in random order if they are not fired by Element:fireEvent." – WhiteRau Jan 05 '12 at 14:59
  • so, being late and loaded with caffeine, i just about filled my pants. having read your answer, i see that it is NOT that IE WON'T fire an event period, but that it will choose when the event fires unless directed by fireEvent. is that correct? – WhiteRau Jan 05 '12 at 15:00
  • For the purposes of this exercise, you probably don't need to invoke fireEvent at all. You only need to use it if you need to emulate a user action (i.e. a click) without the user having to do it themselves. A trivial example would be a row of checkboxes that each have their own click handler. A "select all" checkbox could be made by simply iterating through all of the existing checkboxes and firing their click events. (There are better ways to do this, but they are outside the scope of this comment). – Julian H. Lam Jan 05 '12 at 15:37
  • ah! i understand now. thank you @Julian! :D you and @Dmimitar are like pocket genies! :) WR! – WhiteRau Jan 05 '12 at 18:40
  • Here's to learning. @Dimitar's far more knowledgeable about all this than I! – Julian H. Lam Jan 09 '12 at 15:23