Solved

com event handler in javascript vs. vbscript

Posted on 2013-12-16
8
400 Views
Last Modified: 2013-12-31
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
Comment
Question by:dhenderson12
  • 3
  • 3
  • 2
8 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39723282
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
 

Author Comment

by:dhenderson12
ID: 39723893
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
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39723995
This would only work in ie - and would need permission by the user - did you look in the console?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 

Author Comment

by:dhenderson12
ID: 39736283
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
 
LVL 43

Expert Comment

by:Rob
ID: 39737027
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
 

Author Comment

by:dhenderson12
ID: 39737917
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
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 39738395
Almost the same way

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

obj.onSomeEvent=function() {
  alert("someEvent triggered");
}
0
 
LVL 43

Expert Comment

by:Rob
ID: 39739347
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question