Solved

com event handler in javascript vs. vbscript

Posted on 2013-12-16
8
392 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
Comment Utility
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
Comment Utility
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
Comment Utility
This would only work in ie - and would need permission by the user - did you look in the console?
0
 

Author Comment

by:dhenderson12
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
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
Comment Utility
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
Comment Utility
Almost the same way

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

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

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now