Solved

How to run a Javascript function from VB and then get the return value?

Posted on 2008-10-15
7
658 Views
Last Modified: 2010-04-21
Hi! :)

I currently have many libraries written in JavaScript and I do not want to translate them to Visual Basic 6.0. (NOT .net)

I need a way to call a JS function from VB and get the return value of the function back to VB.

I have been investigating and the following references might have part of the answer, but not all:
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_21057068.html?query=execScript&clearTAFilter=true
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Controls/Q_21559213.html
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21515401.html

The above references say how to execute JS code from VB, but what I am looking is for is to execute JS and get the values back to VB, which is NOT found there (or not clear enough)

I will award full points to any user who can provide me the JS and VB working code that would implement the following JS function - the simplest function ever-, being called from VB, and getting the result of the function back from JS to VB:

function add(x,y)
{
return x+y;
}

That is: VB should be able to send parameters to JS function, and then get the result.

Thanks to everyone in advance. Your help will be really useful! :)
Thanks!!!
0
Comment
Question by:rodrix385
  • 4
  • 3
7 Comments
 
LVL 27

Expert Comment

by:BigRat
ID: 22730245
What you do is to create a ScriptControl object, load your code into it, attach a few events and then run it.

This tutorial might get you started :- http://rookscape.com/vbgaming/tutBQ.php
There's one from Microsoft on calling functions and getting a result :-

http://support.microsoft.com/kb/184740/en-us
0
 

Author Comment

by:rodrix385
ID: 22868427
Hi BigRat,
Your answer was INCREDIBLE!!!!

You gave me the BEST solution ever! I am so happy and so greatful.

However, I am still having a little problem. I went through your tutorial which explained how to add new classes and modules to the VBS Script.

However, using JS Script, I can't seem to manage to do that. I get an error saying "Cannot not add object because it's empty" (refferiing to my new Class with additional commands).

Any clues?

Anyways, what I am trying to do is to get the ERROR information from Javascript. Now when I get an error in the JS code, it just says error, and I am looking for the Error Description + Line Number.

Could you help me on how to set that up? That would make the JS parser completely functional :) :) :)

I have searched everywhere on the internet but this topic seem unknown... so I guess you're the only one who can help me.

I have increased the Point Value.

I really appreciate your help.
Thanks for everything!
Rod
0
 
LVL 27

Accepted Solution

by:
BigRat earned 200 total points
ID: 22869193
1. Error

The ScriptControl supports an Event OnError, which you can set an event handler. I'm not sure but it's probably IConnection.

The control supports an Error property which returns an IScriptError interface. The properties Description,Line and Column of that interface will give you what you want.

2. Problems with JS

I'd ask you to post or send me your code and I'll look through it.

Tip: the control resides is msscript.ocx which is probably in System32. It you have the OLEViewer you can view the type library of this control and see what properties and methods are available (if you understand a bit about IDL).
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:rodrix385
ID: 23994538
BigRat!

I can't believe how I left this question open!

Thank you for your wonderful answer. Thanks to you my VB and JS programming has changed completely.
THANK YOU THANK YOU THANK YOU!

In order to make up for the delay I am increasing the points and assigning all of them to you.

THANKS!
Best,
Rod
0
 

Author Closing Comment

by:rodrix385
ID: 31506625
THANK YOU THANK YOU THANK YOU!!!
0
 
LVL 27

Expert Comment

by:BigRat
ID: 23999606
Rat always waits patiently for cheese!
0
 

Author Comment

by:rodrix385
ID: 24033538
Thanks Bigrat!!!


I have another question specifically for you on this same subject :)

Check it here:
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_24282741.html

Thanks!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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 …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

815 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

8 Experts available now in Live!

Get 1:1 Help Now