Solved

Getting return value from Javascript function via webbrowser control in VB

Posted on 2004-10-20
5
3,639 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

747 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

14 Experts available now in Live!

Get 1:1 Help Now