开发者

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 = d开发者_如何学JAVAocument.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);


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++;
  };
}


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.


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


In IE11 document.dispatchEvent still doesn't work, but now attachEvent is missing too, so the other solution is not going to work either. However, I came up with one even uglier. :) It involves replacing the addEventListener method and goes on like this:

var oldEventListener = document.addEventListener;

document.addEventListener = function (event, func, capture) {
    if (event == "MyPreciousCustomEvent") {
        document.MyPreciousCustomEvent = func;
    }

    oldEventListener.call(document, event, func, capture);
};

...

$(function () {
    try {
        document.MyPreciousCustomEvent("MyPreciousCustomEvent", {});
    } catch (e) {}
});

Hope this helps someone.


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.


Yeah referring to @Don Albrecht, you can use jquery trigger() method more on http://api.jquery.com/trigger/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜