Search code examples
javascriptcom

Javascript and COM intereaction


I am developing an application to communicate between javascript(front end) and COM Interface. Can someone tell me how to invoke the COM interface method in javascript ?.

Appreciate your response.


Solution

  • Calling a COM object from JavaScript will only work in ActiveX compatible browsers, namely Internet Explorer. Other browsers are unsupported, however there are third party extensions available, which may enable ActiveX awareness. Furthermore, your COM interface will need to need to be able to serve late bound clients, such as JavaScript in your case. This can be achieved by either declaring a dispinterface or implementing a dual interface via IDispatch.

    From there, it's pretty straightforward to use a COM object:

    <html>
        <head>
            <script type="text/javascript">
                function test(){
                    alert("Hello");
                    var obj = new ActiveXObject("Math.MathCls");
                    var res = obj.Add(3,4);
                    alert("The answer is " + res);  
                }   
            </script>
        </head>
        <body>
            <button type="button" onclick="test()">Click me</button>
        </body>
    </html>