Link to home
Start Free TrialLog in
Avatar of james henderson
james hendersonFlag for United States of America

asked on

com event handler in javascript vs. vbscript

I have created a com object that raises events.  It works and I have been able to test it in vbs.  the events are raised like this:

set obj = WScript.CreateObject("TestObj.Agent", "agent_")  'create the object

Sub agent_AgentEvent(val)
      MsgBox "Event fired - val is " & val
End Sub

the event handler is a sub in vbscript.  how do I do this in javascript?
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Without having used WScript.CreateObject myself, I would translate it to this

var obj = WScript.CreateObject("TestObj.Agent", "agent_");  //create the object

function agent_AgentEvent(val) {
  alert("Event fired - val is " + val);
}
Avatar of james henderson

ASKER

thanks for the response.  I ask the question incorrectly. I was looking for a way to translate the sub procedure that vbscript uses for the event handler to javascript.  I am trying to put this com object on a web page, but used windows script host to test my object first.

that being said, I put the object on the web page and cannot get the events.  here is my code:

<script type="text/javascript">
        
        var ica = new ActiveXObject("TestObj.Agent");
        var ok = ica.Init();
        var sm;

        if (ok)
            sm = "OK";
        else
            sm = "Failed";

        alert(sm);

        ica.Monitor();

        ica.SetAgentState("Available", ica.NewSessionId);

        function ica_AgentEvent(val) {
            alert("Event fired - val is " + val);
        }
        
    </script>

Open in new window

This would only work in ie - and would need permission by the user - did you look in the console?
sorry, all, for the delay in answering.  this is an intranet site and is limited to IE, so that's not an issue.

bottom line is my com object raises events that can be handled by client side vbscript, but I would like to have the event handler in javascript and I don't know how to do that.  I have posted what i have tried above and can't get any further than that.  how can I get an event handler for the com object in javascript?
This shows how instantiate your ActiveX object using JavaScript.
http://msdn.microsoft.com/en-us/library/ie/7sw4ddf8(v=vs.94).aspx

However, it would worth using following tool to make sure your object reference is correct. When you'll run it you'll be able to see the objects available to ie.
http://msdn.microsoft.com/library/d0kh9f4c.aspx
thx for the reply, tagit, but that's not the question.  I can instantiate the object and call the exposed methods and properties on the object just fine.  the question is event handling.

the object is put on the page using the <object> tag and guid.
the object raises events.
the event handler is written in vbscript on the page and works just fine.

the question is how can I get the event handler in javascript instead of vbscript?
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It sounded from your comments you wanted to instantiate the object using javascript.  

Assuming the object is inserted into the page in your object tags, you should still be able to capture the events with javascript as mplungjan has indicated.

Can you post your object tag and the events you want javascript to capture.

I have to ask though, why do you want to use javascript when vbscript works fine??  This is only going to work in IE as has already been said.