How to handle an ActiveX event in Javascript

后端 未结 7 1341
半阙折子戏
半阙折子戏 2020-11-28 07:58

This is somewhat of a follow-up to an answer here.

I have a custom ActiveX control that is raising an event (\"ReceiveMessage\" with a \"msg\" parameter) that needs

相关标签:
7条回答
  • 2020-11-28 09:00

    In my case, I needed a way to dynamically create ActiveX controls and listen to their events. I was able to get something like this to work:

    //create the ActiveX
    var ax = $("<object></object>", {
        classid: "clsid:" + clsid,
        codebase: install ? cabfile : undefined,
        width: 0,
        height: 0,
        id: '__ax_'+idIncrement++
    })
    .appendTo('#someHost');
    

    And then to register a handler for an event:

    //this function registers an event listener for an ActiveX object (obviously for IE only)
    //the this argument for the handler is the ActiveX object.
    function registerAXEvent(control, name, handler) {
        control = jQuery(control);
    
        //can't use closures through the string due to the parameter renaming done by the JavaScript compressor
        //can't use jQuery.data() on ActiveX objects because it uses expando properties
    
        var id = control[0].id;
    
        var axe = registerAXEvent.axevents = registerAXEvent.axevents || {};
        axe[id] = axe[id] || {};
        axe[id][name] = handler;
    
        var script =
        "(function(){"+
        "var f=registerAXEvent.axevents['" + id + "']['" + name + "'],e=jQuery('#" + id + "');"+
        "function document." + id + "::" + name + "(){"+
            "f.apply(e,arguments);"+
        "}"+
        "})();";
        eval(script);
    }
    

    This code allows you to use closures and minimizes the scope of the eval().

    The ActiveX control's <object> element must already be added to the document; otherwise, IE will not find the element and you'll just get script errors.

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