How to Fire Personal Event in Javascript

前端 未结 6 1886
闹比i
闹比i 2020-12-13 11:15

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

My code is:

var evento; 
if(document.createEventObject)  
{  
   evento         


        
相关标签:
6条回答
  • 2020-12-13 11:45

    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

    0 讨论(0)
  • 2020-12-13 11:48

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

    0 讨论(0)
  • 2020-12-13 12:00

    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++;
      };
    }
    
    0 讨论(0)
  • 2020-12-13 12:08

    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.

    0 讨论(0)
  • 2020-12-13 12:11

    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.

    0 讨论(0)
  • 2020-12-13 12:12

    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.

    0 讨论(0)
提交回复
热议问题