How to handle an ActiveX event in Javascript

后端 未结 7 1340
半阙折子戏
半阙折子戏 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 08:40

    I found this code works within a form tag. In this example, callback is a function parameter passed in by javascript to the ActiveX control, and callbackparam is a parameter of the callback event generated within the activeX control. This way I use the same event handler for whatever types of events, rather than try to declare a bunch of separate event handlers.

    <object id="ActivexObject" name="ActivexObject" classid="clsid:15C5A3F3-F8F7-4d5e-B87E-5084CC98A25A"></object>

    <script>
    function document.ActivexObject::OnCallback(callback, callbackparam){
    callback(callbackparam);
    }
    </script>

    0 讨论(0)
  • 2020-11-28 08:44

    I have used activex in my applications before. i place the object tags in the ASP.NET form and the following JavaScript works for me.

    function onEventHandler(arg1, arg2){
        // do something
    }
    
    window.onload = function(){
        var yourActiveXObject = document.getElementById('YourObjectTagID');
        if(typeof(yourActiveXObject) === 'undefined' || yourActiveXObject === null){
            alert('Unable to load ActiveX');
            return;
        }
    
        // attach events
        var status = yourActiveXObject.attachEvent('EventName', onEventHandler);
    }
    
    0 讨论(0)
  • 2020-11-28 08:47

    I think that the MyControl::ReceiveMessage example does not work because the ActiveX control is being exposed with a different name or in a different scope.

    With the example GetControl::ReceiveMessage, I believe that the function definition is being parsed before the GetControl reference is being set, thus it does not refer to a valid object and cannot bind the function to the object.

    I would attack this problem by using the MS script debugger and trying to determine if a default reference for the control exists with a different name or in a different scope (possibly as a child of the form). If you can determine the correct reference for the control, you should be able to bind the function properly with the Automagic :: method that the MSDN article specifies.

    One more thought, the reference may be based on the name of the object and not the ID, so try setting both :)

    0 讨论(0)
  • 2020-11-28 08:49

    I was able to get this working using the following script block format, but I'm still curious if this is the best way:

    <script for="MyControl" event="ReceiveMessage(msg)">
        alert(msg);
    </script>
    
    0 讨论(0)
  • 2020-11-28 08:52

    OK, but if you are using C# (.NET 2.0) with inherited UserControl (ActiveX)... The only way to make it work is by "Extending" the event's handler functionality: http://www.codeproject.com/KB/dotnet/extend_events.aspx?display=Print

    The above project link from our friend Mr. Werner Willemsens has saved my project. If you don't do that the javascript can't bind to the event handler.

    He used the "extension" in a complex way due to the example he chose but if you make it simple, attaching the handle directly to the event itself, it also works. The C# ActiveX should support "ScriptCallbackObject" to bind the event to a javascript function like below:

      var clock = new ActiveXObject("Clocks.clock");
      var extendedClockEvents = clock.ExtendedClockEvents();
      // Here you assign (subscribe to) your callback method!
      extendedClockEvents.ScriptCallbackObject = clock_Callback; 
      ...
      function clock_Callback(time)
      {
        document.getElementById("text_tag").innerHTML = time;
      }
    

    Of course you have to implement IObjectSafety and the other security stuff to make it work better.

    0 讨论(0)
  • 2020-11-28 08:53

    If you have an ActiveX element on your page that has an id of 'MyControl' then your javascript handler syntax is this:

    function MyControl::ReceiveMessage(msg)
    {
       alert(msg);
    }
    
    0 讨论(0)
提交回复
热议问题