views:

306

answers:

4

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);

A: 

As I read the relevant MSDN article page on the createEventObject method, it appears as though it isn't used for creating custom event - it is used for creating custom objects that can be passed to already existing events.

Description: Generates an event object to pass event context information when you use the fireEvent method.

http://msdn.microsoft.com/en-us/library/ms536390%28VS.85%29.aspx

Update: You are getting the "invalid arguments" error because 'eventoPersonal' is not an acceptable event to fire.

Goyuix
Thanks, but I do not know why I forget "NO", because I really pregunts "How do I fire NO custom events in Internet Explorer?"
matias albala
+1  A: 

You may want to consider using a library to abstract this. Both prototype an jquery will handle this for you. Jquery is especially good at allowing you to create an event with very simple code.

Jquery's documentation is available here: http://docs.jquery.com/Events

Don Albrecht
Yes, but HOW do these frameworks implement custom event names in IE?
Jason
A: 

I think the answer is - in IE you can not fire events that are not on this list:

MSDN - DHTML Events

From what I can gather, frameworks store a registry of the "custom" event names and you must use their implementation specific trigger and handle functions for custom events. For example, prototype uses the ondatavailable event to pass through their custom events behind the scenes.

Jason
+8  A: 

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++;
  };
}
RueTheWhirled
+1 This is EXACTLY what he is asking about, and even though it's technically a workaround, it is exactly what the original poster was asking for. (good find!)
altCognito
+1 I repeat @alt
galambalazs
Great answer, I was actually looking at prototype to see how they did it (see answer below) but this is a great example and article! +50 rep awarded (when SO lets me, says 3 hours...) :D
Jason
Glad it helped.
RueTheWhirled