Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

com event handler in javascript vs. vbscript

Posted on 2013-12-16
8
Medium Priority
?
407 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 2000 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

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…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

715 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