Link to home
Start Free TrialLog in
Avatar of MatteusX
MatteusX

asked on

Getting return value from Javascript function via webbrowser control in VB

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
Avatar of YSkelling
YSkelling

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.
Avatar of MatteusX

ASKER

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.
He found it, he keeps the points !
ASKER CERTIFIED SOLUTION
Avatar of RomMod
RomMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial