Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Fire Personal Event in Javascript

I can't fire personal events using Javascript in IE. In Firefox work great.

My code is:

var evento; 
if(document.createEventObject)  
{  
   evento = document.createEventObject();  
   document.fireEvent('eventoPersonal', evento);     
}  
//FF  
else  
{  
    evento = document.createEvent('Events');  
    evento.initEvent('eventoPersonal',true,false);  
    document.dispatchEvent(evento);  
}

But when try to execute document.fireEvent('eventoPersonal', evento); in IE, it doesn't work. How can I fire NO custom events in IE?

In Internet Explorer I get the error: "Invalid arguments" in the line where execute document.fireEvent('eventoPersonal', evento);

like image 986
matias albala Avatar asked Jan 08 '10 15:01

matias albala


People also ask

Which function is used to fire an event?

To fire an event, use the emit() method.

What is dispatchEvent in Javascript?

The dispatchEvent() method of the EventTarget sends an Event to the object, (synchronously) invoking the affected EventListener s in the appropriate order. The normal event processing rules (including the capturing and optional bubbling phase) also apply to events dispatched manually with dispatchEvent() .

How do you create an event object?

The createEvent() method creates an event object. The event must be of a legal event type, and must be initialized (dipatched) before use.


1 Answers

Dean Edward's describes how to fire cutsom events in IE

http://dean.edwards.name/weblog/2009/03/callbacks-vs-events/

Its near the bottom of the article

var currentHandler;

if (document.addEventListener) {

  // We've seen this code already

} else if (document.attachEvent) { // MSIE

  document.documentElement.fakeEvents = 0; // an expando property

  document.documentElement.attachEvent("onpropertychange", function(event) {
    if (event.propertyName == "fakeEvents") {
      // execute the callback
      currentHandler();
    }
  });

  dispatchFakeEvent = function(handler) {
    // fire the propertychange event
    document.documentElement.fakeEvents++;
  };
}
like image 86
Okeydoke Avatar answered Sep 28 '22 08:09

Okeydoke