Solved

Getting return value from Javascript function via webbrowser control in VB

Posted on 2004-10-20
5
3,768 Views
Last Modified: 2008-01-09
I have spent a few days on this issue so far with no results.  I have a Web Browser control in Visual Basic (I am using VB 6, but have access to newer versions if needed) and would like to get the return value of a Javascript function (contained in a document loaded in the web browser control) back to VB.  I have looked at execScript, with something like:

web1.Document.parentWindow.execScript "test()", "javascript"

which does successfully execute the test function in web1's current document.  However, as indicated on the MSDN reference for execScript, getting the return value of the function is not possible. Unfortunately, I do not have access to modify the web page in question, so any manipulation on that side of things is not possible (such as having the function store the result in a global, and then pulling that variable back to VB).

I have read an interesting article "How To Call a Script Function from a VC WebBrowser Application" at
http://support.microsoft.com/kb/q185127/.  The code in the article does exactly what is needed, but the example is in Visual C++ and performing something similar in VB, if it's even possible, is beyond me.

Any help would be much appreciated.

Thank you,
Matt
0
Comment
Question by:MatteusX
  • 2
5 Comments
 
LVL 2

Expert Comment

by:YSkelling
ID: 12366286
Sorry, but you could compile this C++ function in a dll, then call it from your program ? ( With an EXPORT )

( easy answer... but simple )

Next info :

You can add a reference to mshtml.tlb in you vb application, the try to do what the C++ example does ?

Yan.
0
 

Author Comment

by:MatteusX
ID: 12377828
I do not have experience making DLLs, but it is a good idea.  I would like to stick with an all VB solution.  Thank you for the suggestion.  

I actually just solved it myself.  In my problem I mentioned storing a result of a function in a global variable.  At first I thought since I didn't have access to the javascript, I couldn't set a global.  But, this works, even though it's not very elegant:

---
'web is my webbrowser object.

web.Document.parentWindow.execScript "eval('x=test2()')", "javascript"
x = web.Document.parentWindow.x
msgbox x
---

The test2 function in the document loaded in the webbrowser control is something like:

function test2() {
   return 123;
}

This works for me.  Thank you for your suggestions.
0
 
LVL 2

Expert Comment

by:YSkelling
ID: 12382237
He found it, he keeps the points !
0
 

Accepted Solution

by:
RomMod earned 0 total points
ID: 12414377
The question has been PAQ'd and the 350 points have been refunded.
RomMod
Community Support Moderator
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

679 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