• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 414
  • Last Modified:

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?
0
dhenderson12
Asked:
dhenderson12
  • 3
  • 3
  • 2
1 Solution
 
Michel PlungjanIT ExpertCommented:
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);
}
0
 
dhenderson12Author Commented:
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

0
 
Michel PlungjanIT ExpertCommented:
This would only work in ie - and would need permission by the user - did you look in the console?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
dhenderson12Author Commented:
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?
0
 
RobOwner (Aidellio)Commented:
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
0
 
dhenderson12Author Commented:
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?
0
 
Michel PlungjanIT ExpertCommented:
Almost the same way

var obj = document.getElementById("yourObjectID");
obj.triggerSomeEvent();

obj.onSomeEvent=function() {
  alert("someEvent triggered");
}
0
 
RobOwner (Aidellio)Commented:
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.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now